I have the following:
db.Products.Where(p=>p.Sectors.Where(s=>s.website_id == websiteObj.website_id)).Count();
websiteObj is not null and has valid data. There is a many to many relationship between products and sectors. Also, there is a 1 to many relationship between sectors and websites. I don't have direct access to products from website, I have to go through the website related sectors.
Anyway, this example works: db.Sectors.Where(p=>p.website_id == websiteObj.website_id)).Count();
But the issue is that the first LINQ query gives me the following errors: 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.
And a couple other errors.
Anyway, what am I doing wrong? This is my first attempt with LINQ. Thanks in advance.
I think you need Any
in the inner query:
db.Products.Where(p => p.Sectors.Any(s => s.website_id == websiteObj.website_id))
.Count();
This would return all Products with at least one sector with the specified website_id.
If you want all Products where all sectors belong to the specified website_id, simply use All
instead of Any
:
db.Products.Where(p => p.Sectors.All(s => s.website_id == websiteObj.website_id))
.Count();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.