如何在c#linq中使用一个元素(值)查找键
我有下一本c语言词典#如何在c#linq中使用一个元素(值)查找键,c#,linq,dictionary,C#,Linq,Dictionary,我有下一本c语言词典# Dictionary Dictionary=newdictionary(); {1,1,2,3,4 2, 4,5,6,7 3,1} 我想找到列表中只有一个元素的元素(字典中的最后一个)。 我该怎么做?编辑: 我刚意识到你想找到钥匙 List<int> result = dictionary.Where(x => x.Value.Count == 1).Select(x => x.Key).ToList(); List result=dictio
Dictionary Dictionary=newdictionary();
{1,1,2,3,4
2, 4,5,6,7
3,1}
我想找到列表中只有一个元素的元素(字典中的最后一个)。
我该怎么做?编辑:
我刚意识到你想找到钥匙
List<int> result = dictionary.Where(x => x.Value.Count == 1).Select(x => x.Key).ToList();
List result=dictionary.Where(x=>x.Value.Count==1);
这应该就是你要找的那个
原件:
我相信您正在使用LINQ寻找此类产品:
IEnumerable<List<int>> result = dictionary.Values.Where(x => x.Count == 1);
IEnumerable结果=dictionary.Values.Where(x=>x.Count==1);
如果你想把它放在列表中,我会考虑枚举集合。
List<List<int>> result = dictionary.Values.Where(x => x.Count == 1).ToList();
List result=dictionary.Values.Where(x=>x.Count==1.ToList();
希望这能回答您的问题。这样:
Dictionary<int, List<int>> dictionary = new Dictionary<int, List<int>>();
List<KeyValuePair<int, List<int>>> result = dictionary.Where(keyValuePair => keyValuePair.Value.Count == 1).ToList();
Dictionary Dictionary=newdictionary();
列表结果=dictionary.Where(keyValuePair=>keyValuePair.Value.Count==1.ToList();
发生了什么事
dictionary.Where
|在dictionary
中搜索
keyValuePair=>
|一个keyValuePair
我将用名称keyValuePair
keyValuePair.Value
|哪个有它的值(它是一个列表
)
keyValuePair.Value.Count==1
|且Count
等于1
.ToList()
|然后返回一个包含匹配的键值对的列表
是否需要频繁查找这些元素?还是在一个大的时间跨度内只有一次?
Dictionary<int, List<int>> dictionary = new Dictionary<int, List<int>>();
List<KeyValuePair<int, List<int>>> result = dictionary.Where(keyValuePair => keyValuePair.Value.Count == 1).ToList();