添加子集合的“Where”条件

huangapple go评论49阅读模式
英文:

Add Where condition to child collection

问题

以下是您提供的内容的翻译:

我有两个模型,InvoiceStockItems,其中 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();

huangapple
  • 本文由 发表于 2023年4月13日 19:47:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定