英文:
Add Where condition to child collection
问题
以下是您提供的内容的翻译:
我有两个模型,Invoice 和 StockItems,其中 StockItems 是子模型。我想获取带有其StockItems的发票,其中 StockItem.Used 为True。
这是我使用的查询:
var Invoices = _dbcontext.Invoice
                     .Include(a => a.StockItems)
                     .Where(s => s.StockItems.Any(a => a.Used == true))
                     .ToList();
但是 Where 条件没有应用,查询返回了所有的 StockItems,无论 StockItem.Used 属性的值是True还是False。
那么我的查询中存在什么错误呢?
英文:
I have two models Invoice and StockItems where StockItems is the child model. I would like to get the invoices with their StockItems where StockItem.Used is True.
This is the query I used:
var Invoices = _dbcontext.Invoice
                         .Include(a => a.StockItems)
                         .Where(s => s.StockItems.Any(a => a.Used == true))
                         .ToList();
But the Where condition doesn't get applied, the query returns all the StockItems whatever the value of StockItem.Used property True and False.
So where is the error in my query?
答案1
得分: 0
我成功获取到查询:
var Invoices = _dbcontext.Invoice
                     .Include(a => a.StockItems.Where(s => s.Used == true))
                     .ToList();
英文:
I managed to get the query:
var Invoices = _dbcontext.Invoice
                     .Include(a => a.StockItems.Where(s => s.Used == true))
                     .ToList();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论