1

I am having a hard time figuring this out. The examples out there are not using async calls.

How do I use Include when making a get call for a single object by id?

Do I need to get rid of the async call? I need to include the matching

ExerciseRepetitions && ExerciseImages

Also how could I add a

.Where(k => k.IsHidden != true)

for each child model?

[Route("")]
public IQueryable<Exercise> GetExercises()
{
    var result = db.Exercises
        .Include(c => c.ExerciseRepetitions)
        .Include(o => o.ExerciseImages)
        .Where(k => k.IsHidden != true);
    return result;
}

// GET: api/Exercises/5
[Route("{id:int}")]
[ResponseType(typeof(Exercise))]
public async Task<IHttpActionResult> GetExercise(int id)
{
    Exercise exercise = await db.Exercises.FindAsync(id);

       ///// I need this call to return ExerciseRepetitions &&  ExerciseImages  as nested arrays. Also I need to filter out any object that has  IsHidden = true.

    if (exercise == null)
    {
        return NotFound();
    }

    return Ok(exercise);
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
texas697
  • 5,609
  • 16
  • 65
  • 131

1 Answers1

2

You can't filter entities through the Include extension method.

You can do the following by explicit loading the collection properties. Explicit loading let you filter the collection that you're loading like this:

if(exercise != null)
{
    await db.Entry(exercise)
             .Collection(p => p.ExerciseRepetitions)
             .Query()
             .Where(p => !p.IsHidden)
             .LoadAsync();

    await db.Entry(exercise)
             .Collection(p => p.ExerciseImages)
             .Query()
             .Where(p => !p.IsHidden)
             .LoadAsync();
}
CodeNotFound
  • 22,153
  • 10
  • 68
  • 69