我有以下内容:
db.Products.Where(p=>p.Sectors.Where(s=>s.website_id == websiteObj.website_id)).Count();
websiteObj不为空并且包含有效数据。产品和行业之间存在多种关系。此外,部门和网站之间存在一对多的关系。我没有从网站直接访问产品,我必须通过网站相关部门。
无论如何,这个例子有效:
db.Sectors.Where(p=>p.website_id == websiteObj.website_id)).Count();
但问题是第一个LINQ查询给出了以下错误:
Delegate 'System.Func<BusinessObjects.Product,int,bool>' does not take 1 arguments.
Cannot convert lambda expression to type 'string' because it is not a delegate type.
还有其他几个错误。
无论如何,我做错了什么?这是我第一次使用LINQ。 提前谢谢。
答案 0 :(得分:2)
我认为你在内部查询中需要Any
:
db.Products.Where(p => p.Sectors.Any(s => s.website_id == websiteObj.website_id))
.Count();
这将返回包含至少一个具有指定website_id的扇区的所有产品。
如果您希望所有所有行业的产品都属于指定的website_id,只需使用All
代替Any
:
db.Products.Where(p => p.Sectors.All(s => s.website_id == websiteObj.website_id))
.Count();