这是我使用的通用存储库,正如标题所述,我想知道如何过滤导航属性。
public IEnumerable<T> Query(
Expression<Func<T, bool>> filter = null,
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
string includeProperties = "")
{
IQueryable<T> query = _objectSet.Where(e => !e.IsDeleted);
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
控制器:
var viewModel = new StudentViewModel();
viewModel.Students= _unitOfWork.Students.Query(
includeProperties: "Subjects, Instructors");
现在我的问题是我想使用存储库向 [Subjects] 和 [Instructors] 添加 .Where(e => !e.IsDeleted) 。
谢谢
编辑:根据 Ladislav,目前这是不可能的(在 msdn 中也提到过:http: //blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature- ctp5-part-6-loading-related-entities.aspx )
Can i just use this instead?
viewModel.Subjects = viewModel.Students.Where(i => i.StudentID ==Id.Value)
.Single().Subjects.Where(e => !e.IsDeleted);
我唯一担心的是查询可能会返回大量 isDeleted==true 的记录。当然我发布的代码作为替代工作,我只是不想提取我不需要的数据,即使我可以使用上面的代码过滤它