简体   繁体   English

Linq To Sql子查询复合体

[英]Linq To Sql Sub Query Complex

tblItem tblItem

  • Name 名称
  • ProductID 产品ID

tblProduct tblProduct

  • Name 名称
  • ProductID 产品ID
  • CategoryID 类别ID

tblCategory tblCategory

  • Name 名称
  • CategoryID 类别ID

     var itms = from item in CMP.tblItems let t2s = (from g in CMP.tblProducts where g.CategoryID==CatID select g.ProductID) where item.Name.Contains(Model) && item.ProductID.ToString() == t2s.ToString() select new { item.Name }; 

My problem is more than one product is return to t2s (Sub-Query). 我的问题是不止一种产品返回到t2s(子查询)。 if i add FirstOrDefault() to Sub-Query then it will match it with only one product id! 如果我将FirstOrDefault()添加到子查询中,则它将仅与一个产品ID相匹配! i need to match will all productid(s) it returns. 我需要匹配将返回的所有productid。

Try This One: 试试这个:

var itms=from item in CMP.tblItems
         from g in CMP.tblProducts
         where item.Name.Contains(Model) && item.ProductID == g.ProductID && g.CategoryID == CatID
         select new {item.Name};

Use the navigation properties that LINQ-to-SQL creates for you. 使用LINQ-to-SQL为您创建的导航属性。 Item should have property Product . Item应具有属性Product So you can simply do this: 因此,您可以简单地执行以下操作:

var itms = from item in CMP.tblItems
    where item.Name.Contains(Model) && item.Product.CategoryID = CatId
    select new { item.Name };

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

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