使用linq检查ILookup <string,string>中的键是否存在值的最佳方法

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的新手,所以学习更好的方法

p.s*_*w.g 7

ILookup<TKey, TElement>.Item属性返回的对象(当你这样做时就是这个对象someList[...])是一个IEnumerable<TElement>.因此,您可以将每个项目直接与测试值进行比较.像这样:

var status = someList["Football"].Any(y => y == "Venus Williams");
Run Code Online (Sandbox Code Playgroud)

  • @MarcinJuraszek你确定吗?我只是尝试使用`ToLookup`创建的查找,它返回一个空集.请注意[`IDictionary`](http://msdn.microsoft.com/en-us/library/zyxt2e2h.aspx)文档指定了一个异常,但是[`ILookup`](http://msdn.microsoft.com /en-us/library/bb549314.aspx)docs没有. (7认同)

AD.*_*Net 2

bool status = someList.Where(x => x.Key == "Football").Any( y => y.Value == "Venus williams")
Run Code Online (Sandbox Code Playgroud)