I have a object IQueryable which I am trying to dynamically add Where clauses, this works great for columns on Listing object, however now conditionally I would like to add a IN clause to a navigation property (ListingAmenities) which has columns ListingID, AmenityID
I have critiera.AmenityIDs which may contain Amenities I like to filter the results on.
I am trying to achieve the following if there is any amenityIDs in my array
select * from Listings l inner join ListingAmenities a on l.ListingID = a.ListingID where a.AmenityID IN(1,2,3)
here is my code (I am using EF5)
if (criteria.AmenityIDs.Count > 0)
{
listings = listings.Where(x => x.ListingAmenities.Any(y => y.AmenityID == criteria.AmenityIDs));
}
which of course does not work. Important note is that I am adding these WHERE clauses dynamically so that is why I am building up a IQueryable object