I have a small LINQ query containing a Where clause; something like:
var blueRedGreenBikes = GetBikes(filter)
.Where(a => a.Color == "Blue" || a.Color == "Red" || a.Color == "Green")
.Count()
I am looking for a way to write this type of query more concisely using LINQ. In SQL, I might write a similar WHERE clause like:
WHERE bike.Color IN ('Red','Blue','Green')
You can use the LINQ Contains
method to check your item's color against a collection.
var colorList=new List<string> { "Blue","Red","Green"};
var blueRedGreenBikes = GetBikes(filter).Where(a => colorList.Contains(a.Color)).Count();
Another version
var blueRedGreenBikes = GetBikes(filter).Count(s => colorList.Contains(s.Color));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With