我遇到Include功能问题。我有一个Team类,其Owner属性为Owner。我有一个辅助函数,它包含我的EF调用,如下所示;
public Task<List<T>> GetManyAsync(
Expression<Func<T, bool>> filter = null,
Expression<Func<T, object>> includeProperties = null)
{
IQueryable<T> query = _dbSet;
if (filter != null)
{
query = query.Where(filter);
}
if (InstanceHelper.IsSomething(includeProperties))
{
query.Include(includeProperties);
}
return query.ToListAsync();
}
我像这样使用它
var teams = await DataAccess.Team.GetManyAsync(e => e.Owner.Id == userId, e => e.Owner);
但它返回具有NULL Owner属性的团队列表。知道我在这里缺少什么吗?
答案 0 :(得分:2)
您必须使用此
public Task<List<T>> GetManyAsync(Expression<Func<T, bool>> filter = null, params Expression<Func<T, object>>[] includeProperties = null)
{
foreach (var prop in includeProperties)
query = query.Include(prop);
...
}
你可以拥有多个包含
GetManyAsync(filter ,p => p.prop1 ,p.prop2,...)