我很难搞清楚这一点。那里的例子没有使用异步调用。
如何通过id对单个对象进行get调用时使用Include
?
我是否需要摆脱异步调用?我需要包含匹配的
ExerciseRepetitions && ExerciseImages
另外我怎么能添加
.Where(k => k.IsHidden != true)
每个儿童模特?
[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);
}
答案 0 :(得分:2)
您无法通过Include
扩展程序过滤实体。
您可以通过显式加载集合属性来执行以下操作。显式加载允许您过滤您正在加载的集合,如下所示:
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();
}