Best way to check if key value exists in ILookup <string, string> using linq
ILookup<string, string> someList; Cricket Sachin Dravid Dhoni Football Beckham Ronaldo Maradona bool status = someList.Where(x => x.Key == "Football").Where( y => y.Value == "Ronaldo") should return true
bool status = someList.Where(x => x.Key == "Football").Where( y => y.Value == "Venus williams") should return false
ILookup does not have a value property, instead of looping through a loop, there is a smarter way to get the result in multiple lines. the above code is wrong, hoping for something like this if possible. I am new to Linq, so I’m learning the best ways.
+6
4 answers
The object returned from the ILookup<TKey, TElement>.Item (this is what someList[...] called when someList[...] is executed) is IEnumerable<TElement> . This way you can compare each element directly with your test value. Like this:
var status = someList["Football"].Any(y => y == "Venus Williams"); +7