EF 查询:如何约束嵌套项
请帮我写一个 EF 查询。
我有下表:
Products
ProductId
Name
Items
ProductId
Cost
如何选择名称为“AAA”的产品并且仅选择成本等于 100 的商品?
我写了以下内容:
ctx.Products.Include("Items").Where(p=>p.Name == "AAA" && p.Items.Any(i=>i.Cost == 100)).FirstOrDefault()
但结果我得到了名为“AAA”的产品和所有项目。
谢谢, 德米特里
Please help me write a query for EF.
I have the following tables:
Products
ProductId
Name
Items
ProductId
Cost
How to select product with name 'AAA' and ONLY with items which cost equals to 100?
I wrote the following:
ctx.Products.Include("Items").Where(p=>p.Name == "AAA" && p.Items.Any(i=>i.Cost == 100)).FirstOrDefault()
but as a result I got Product with name "AAA" and with ALL items.
Thanks,
Dmitriy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是因为您正在加载产品的所有项目。无论如何,我会尝试采用其他方式,即主查询可能是这样的
所有这些项目都应该具有相同的产品,如果需要,您也可以急切地加载该产品。
That's because you are loading all items for products. In any case, I'd try going the other way, i.e. the main query might be something like this
All these items should have the same product and you can eager load the product too if you need.