Here is the problem:
I need to return a collection of objects with filtered nested collections.
E.g: there is a store with orders and I need to return a collection with stores that includes nested collections with orders but without orders from customers that are marked as deleted.
Here is what I try to do. But still no luck. Any suggestions are appreciated :)
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;
}
The problem is that the filter is not applied. Customers that have deleted flag set as true stay in the collection.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…