You can use the Contains method array / list / hashset.
var colors = new List<string> {"Green", "Red", "Blue" }; db.Table.Where(x => x.FavoriteSport == "Baseball" && (colors.Contains (x.FavoriteColor)).Count()
It will generate an SQL query, for example
SELECT ... WHERE FavoriteColor = 'Baseball' AND FavoriteColor in ("Green", "Red", "Blue")
I would like to add that if you are working with data sets that are stored in memory, you must remember that List Contains
performs O (N) iteration to get the result. Therefore, if colors
contains many elements, you should instead use a HashSet instead of O (1).
var colors = new HashSet<string> {"Green", "Red", "Blue", .... }; someDataSet.Where(x => x.FavoriteSport == "Baseball" && (colors.Contains (x.FavoriteColor)).Count()
You can find List-HashSet performance comparison here.
source share