Can anyone help me with the following? I'm using Linq to entities (code first).
I have a Products object which is mapped (1 to many) in my db context to my ProductAreas Object.
This query works and brings me back a list of enabled products including their associated areas:
IQueryable<Product> products = from Product in db.Product.Include(p=>p.ProductAreas)
where (Product.BrandId == brandid && Product.Enabled == true)
select Product;
I want to add in a filter so that I'm also only getting back ProductAreas which are enabled.
This is what I initially tried:
IQueryable<Product> products = from Product in db.Product.Include(p=>p.ProductAreas.Where(pa=>pa.Enabled == true))
where (Product.BrandId == brandid && Product.Enabled == true)
but when I run my code I get the following error: "The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path"
I presume I want to add the filter in my where somehow? I tried this but it only brings me back products which have areas (and still bring backs ProductAreas which are not enabled)
IQueryable<Product> products = from Product in db.Product.Include(p=>p.ProductAreas)
where (Product.BrandId == brandid && Product.Enabled == true && Product.ProductAreas.FirstOrDefault(a=>a.Enabled == true) != null)
select Product;
Any help very much appreaciated (as you can probably tell I'm just learning Linq to Entities!!)