这里是问题: 我需要返回一个包含已筛选嵌套集合对象的集合。 例如:有一个商店,有订单,我需要返回包含有订单的商店集合,但不包括被标记为删除的顾客订单。
这是我尝试做的事情。但是还没有成功。欢迎提出建议 :)
public List<StoreEntity> GetStores(Func<Store, bool> storeFilter, Predicate<OrderEntity> orderFileter)
{
IQueryable<StoreEntity> storeEntities = Context.Stores
.Include(o => o.Order)
.Include(cu => cu.Orders.Select(c => c.Customer))
.Where(storeFilter)
//.Where(rcu=>rcu.Orders.Select(cu=>cu.Customer.Deleted==false)) //just test this doesn't work
.AsQueryable();
List<StoreEntity> storeEntities = storeEntities.ToList();
//storeEntities.ForEach(s => s.Orders.ToList().RemoveAll(c=>c.Customer.Deleted==true)); // doesn't work
foreach (StoreEntity storeEntity in storeEntities)
{
storeEntity.Orders.ToList().RemoveAll(r=>r.Customer.Deleted==true);
}
return storeEntities;
}
问题在于筛选器没有起作用。将删除标志设置为True的客户仍然留在集合中。
Z.EntityFramework.Plus.QueryIncludeFilter.EF6
,文档在这里:https://github.com/zzzprojects/EntityFramework-Plus/wiki/EF-Query-IncludeFilter-%7C-Entity-Framework-Include-Related-Entities-using-Where-Filter - Bron Davies