這裏是問題: 我需要返回具有過濾嵌套集合的對象的集合。 例如:有一個有訂單的商店,我需要返回一個商店集合,其中包含帶訂單的嵌套集合,但沒有標記爲已刪除的客戶訂單。如何篩選嵌套的集合實體框架對象?
這是我試圖做的。但仍然沒有運氣。任何建議appriciated :)
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的客戶留在集合中。
問題是什麼?不編譯?它會拋出一個運行時異常嗎?它運行但返回錯誤的數據? –
解釋了一點。謝謝。 –
我最終使用了這個nuget包:'Z.EntityFramework.Plus.QueryIncludeFilter.EF6'文檔在這裏:https://github.com/zzzprojects/EntityFramework-Plus/wiki/EF-Query-IncludeFilter-%7C-Entity- Framework-Include-Related-Entities-using-Where-Filter –