pen*_*bot 10 .net c# linq linq-to-objects dictionary
我有一个字典,看起来像这样:
Dictionary<String, List<String>>
test1 : 1,3,4,5
test2 : 2,3,6,7
test3 : 2,8
Run Code Online (Sandbox Code Playgroud)
如何使用LINQ和LINQ扩展计算所有值?
Ani*_*Ani 34
假设你有:
Dictionary<String, List<String>> dict = ...
Run Code Online (Sandbox Code Playgroud)
如果你想要列表的数量,它就像这样简单:
int result = dict.Count;
Run Code Online (Sandbox Code Playgroud)
如果您想要所有列表中所有字符串的总数:
int result = dict.Values.Sum(list => list.Count);
Run Code Online (Sandbox Code Playgroud)
如果要计算所有列表中所有不同字符串的计数:
int result = dict.Values
.SelectMany(list => list)
.Distinct()
.Count();
Run Code Online (Sandbox Code Playgroud)