0

我有以下内容:

db.Products.Where(p=>p.Sectors.Where(s=>s.website_id == websiteObj.website_id)).Count();

websiteObj 不为 null 并且具有有效数据。产品和部门之间存在多对多的关系。此外,部门和网站之间存在一对多的关系。我不能直接访问网站上的产品,我必须通过网站相关部门。

无论如何,这个例子有效: 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。提前致谢。

4

1 回答 1

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();
于 2012-08-23T07:40:59.633 回答