I have two Entities Store
and Catalog
, having many to many relationship using fluent Api. I want to get a Store by id
with all the catalogs having status equals to "Published".
Below I try to write the following query but not getting the expected results.
var store = context.Stores.Include("Catalogs").Where(s => s.StoreID == id && s.Catalogs.Any(c => c.Status == "Published")).SingleOrDefault();
What you're asking for there is "give me the store with this ID, but only if it has a published catalog" (the "Any" call).
The easiest way to only get published catalogs would be to project them into an anonymous type:
... or, in the fluent interface:
So
result.Catalogs
holds all the published catalogs that apply toresult.Store
.