我有以下几点:
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。提前谢谢。
发布于 2012-08-23 15:41:00
我认为在内部查询中需要使用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();https://stackoverflow.com/questions/12086877
复制相似问题