I have a model Course, which has several many to many relationships like Age or Time.
I have this query:
string IDs = "1,2,3"
string[] IDList = IDs.Split(',');
return (from x in entities.Course
where x.Ages.Where(val => IDList.Contains(val.ID.ToString())).Count() == IDList.Count()
select x);
And I need to set the same query for Time and several other properties as in:
string IDs = "1,2,3"
string[] IDList = IDs.Split(',');
return (from x in entities.Course
where x.Times.Where(val => IDList.Contains(val.ID.ToString())).Count() == IDList.Count()
select x);
How can I make the query more dynamic so I don’t have multiple similar queries?
Thanks
You could make a method that accepts an
Expression(depeneding on your data type) and run the query that way. You’ll need to make yourAges,Time, etc implement a specific interface for it to work.For example, assuming that you are using EF and your model is Code First using
DbSets, you could make this: