Entity framework EF Core:Any()不';t筛选所有项目
我有一个疑问:Entity framework EF Core:Any()不';t筛选所有项目,entity-framework,linq,entity-framework-core,Entity Framework,Linq,Entity Framework Core,我有一个疑问: from supply in context.Supplies where supply.AccountId == accountId && supply.Product.Requests.Any(r => r.WantToTrade && r.PotentialTradeSupplies.Any(p=>p.CanBeTra
from supply in context.Supplies
where
supply.AccountId == accountId
&& supply.Product.Requests.Any(r => r.WantToTrade && r.PotentialTradeSupplies.Any(p=>p.CanBeTraded))
select supply).Include(s => s.Product)
.ThenInclude(p=>p.Requests)
.ThenInclude(r=>r.PotentialTradeSupplies)
.ThenInclude(pts=>pts.Sizes)
.Include(s=>s.Product)
.ThenInclude(p=>p.Requests)
.ThenInclude(r=>r.Account)
.Include(s=>s.Product)
.ThenInclude(p=>p.Gender)
.Include(s=>s.Product)
.ThenInclude(p=>p.Category)
.Include(s => s.Product)
.ThenInclude(p => p.Division)
.Include(s=>s.Product)
.ThenInclude(p=>p.Availabilities)
.ThenInclude(pa=>pa.Sizes)
如果request.WantToTrade为false,则查询将返回产品的所有请求。我只想要WantToTrade为真的请求。如何过滤掉这些内容?首先删除
Any()
过滤器就足够了,这样您的查询就变得简单了
from supply in list
where supply.AccountId == accountId
&& supply.WantToTrade
&& supply.PotentialTradeSupplies.Any(p=>p.CanBeTraded)
select supply
在筛选请求时,您可能应该使用
All
而不是Any
?是否尝试.Include()嵌套子项?我不确定如何在LINQtoSQL中实现这一点,试试流畅的方式,可能会奏效。似乎未加载嵌套项。此查询不返回产品和请求,但提供至少一个与条件匹配的请求。问题中是否未显示使用即时加载(Include
/然后Include
)的查询?顺便说一句,LINQtoSQL和EFCore是完全不同的东西。我更新了代码示例。它包括产品、需求和潜在的贸易物资。就我从现在的阅读中所知,你必须包括你的孩子从上下文中获取物资的权利。i、 e.来自context.Supplies.Inlcude(..)
中的supply,然后您可以按照上面注释中的建议筛选查询。参见下面Tim Schmelter的答案,了解获取数据的另一种方法。