use*_*512 6 c# linq linq-to-entities c#-4.0
ILookup<string, string> someList;
Cricket Sachin
Dravid
Dhoni
Football Beckham
Ronaldo
Maradona
Run Code Online (Sandbox Code Playgroud)
bool status = someList.Where(x => x.Key == "Football").Where( y => y.Value == "Ronaldo")
Run Code Online (Sandbox Code Playgroud)
应该回归真实
bool status = someList.Where(x => x.Key == "Football").Where( y => y.Value == "Venus williams")
Run Code Online (Sandbox Code Playgroud)
应该返回false
ILookup没有value属性,而不是循环,有没有更聪明的方法来获得几行结果.上面的代码是不对的,如果可能的话,希望有类似的东西.我是Linq的新手,所以学习更好的方法
从ILookup<TKey, TElement>.Item属性返回的对象(当你这样做时就是这个对象someList[...])是一个IEnumerable<TElement>.因此,您可以将每个项目直接与测试值进行比较.像这样:
var status = someList["Football"].Any(y => y == "Venus Williams");
Run Code Online (Sandbox Code Playgroud)
bool status = someList.Where(x => x.Key == "Football").Any( y => y.Value == "Venus williams")
Run Code Online (Sandbox Code Playgroud)