简体   繁体   English

LINQ包含查询

[英]LINQ include query

I'm new at LINQ, and i'm normally will resolve this with SQL, but now i'm trying to do the same with linq and i can't figure this out. 我是LINQ的新手,通常我会使用SQL来解决此问题,但是现在我正尝试使用linq进行同样的操作,但我无法弄清楚。 I got 3 tables: 我有3张桌子:

  • Orders (OrderID, Number, etc) 订单(订单ID,编号等)
  • OrdersDetail (OrderID, ProductID, Quantity, etc) OrdersDetail(订单ID,产品ID,数量等)
  • Products (ProductID, Code, Description) 产品(ProductID,代码,说明)

Now i need a list of orders wich contain products matchin a specific code or descripcion string. 现在,我需要一个订单列表,其中包含匹配特定代码或描述字符串的产品。 My SQL will be: 我的SQL将是:

SELECT * FROM Orders WHERE OrderID IN(SELECT OrderID FROM OrdersDetail WHERE ProductID IN(SELECT ProductID FROM Products WHERE Code LIKE 'FilterText%' OR Descripcion LIKE 'FilterText%'))

How do i do this with LINQ? 我如何使用LINQ做到这一点? Also is this the best way to do this, specially if Products table are big? 这也是执行此操作的最佳方法吗,特别是在“产品”表很大的情况下? Thank you! 谢谢!

db.Orders.Where(o=>o.OrderDetails
                        .Any(od=>od.Product.Code.StartsWith("filterText") 
                           || od.Product.Description.StartsWith("filterText")))
          .ToList()

try this: 尝试这个:

var q = (from o in Orders
        from d in o.OrderDetails
        from p in d.Product
        where p.Code.StartsWith("your filter") || p.Description.StartsWith("your filter")
        select o);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM