C# Linq到Sql子查询复合体 tblItem 名字 产品ID TBL产品 名字 产品ID 类别 TBL范畴 名字
类别C# Linq到Sql子查询复合体 tblItem 名字 产品ID TBL产品 名字 产品ID 类别 TBL范畴 名字,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,类别 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.N
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 };
我的问题是不止一个产品返回到t2s(子查询)。如果我将FirstOrDefault()添加到子查询,那么它将只与一个产品id匹配!我需要匹配它返回的所有productid。试试这个:
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};
使用LINQ to SQL为您创建的导航属性<代码>项目应具有属性
产品
。因此,您可以简单地执行以下操作:
var itms = from item in CMP.tblItems
where item.Name.Contains(Model) && item.Product.CategoryID = CatId
select new { item.Name };
你想得到什么结果?如果需要检查item.ProductID是否在t2s中,可以尝试使用
Contains
例如t2s.Contains(item.ProductID)
。您也可以考虑使用<代码>连接> /代码>代替子查询。