在字典中添加字典的 int 值
Adding an int value of a dictionary within a dictionary
我有一本字典,我正在尝试添加另一本字典中的值。
下面是我的字典:
Dictionary<string, Dictionary<string, int>> dic = new Dictionary<string, Dictionary<string, int>>(){
{
"first", new Dictionary<string, int>()
{
{ "test1", 21 },
{ "test2", 1 },
{ "test3", 21 },
{ "test4", 122 },
}
},
{
"second", new Dictionary<string, int>()
{
{ "test1", 33 },
{ "test2", 22 },
{ "test3", 2 },
{ "test4", 1 },
}
},
{
"third", new Dictionary<string, int>()
{
{ "test1", 41 },
{ "test2", 31 },
{ "test3", 12 },
{ "test4", 11 },
}
},
};
我想添加我有键“test1”的所有值。
我试过这样:
int totalTest1 = dic.Sum(x => x.Value.Where(l => l.Key == "test1").SelectMany(z => z.Value));
但是报错
Severity Code Description Project File Line Suppression Status
Error CS0411 The type arguments of the method "Enumerable.SelectMany <TSource, TResult> (IEnumerable <TSource>, Func <TSource, IEnumerable <TResult>>)" cannot be inferred based on usage. Try to explicitly specify the type arguments. CalculadoraLancamento C: \ Users \ sergi \ source \ repos \ CalculadoraLancamento \ CalculadoraLancamento \ Form1.cs 183 Active
您要查找的查询是:
int totalTest1 =
dic
.SelectMany(x => x.Value)
.Where(z => z.Key == "test1")
.Select(z => z.Value)
.Sum();
SelectMany
将外部字典展平为 IEnumerable<KeyValuePair<string, int>>
,这很容易在 Key
上过滤,然后在值上 Sum
上过滤。
根据您提供的示例数据,我得到 95
。
您可能不需要对内部字典执行 LINQ 查询,因为您只是通过键访问字典中的值:
int totalTest1 = dic.Sum(x => x.Value["test1"]);
如果您不确定内部字典是否始终包含指定的键,您可以在访问前检查:
int totalTest1 = dic.Sum(x => x.Value.TryGetValue("test1", out int result) ? result : 0);
获得 95 的另一个变体
Console.WriteLine(dic.SelectMany(x => x.Value)
.Where(x => x.Key == "test1")
.Sum(x => x.Value)); // put criteria into SUM
这实际上比使用 TryGetValue
执行得更好
我有一本字典,我正在尝试添加另一本字典中的值。
下面是我的字典:
Dictionary<string, Dictionary<string, int>> dic = new Dictionary<string, Dictionary<string, int>>(){
{
"first", new Dictionary<string, int>()
{
{ "test1", 21 },
{ "test2", 1 },
{ "test3", 21 },
{ "test4", 122 },
}
},
{
"second", new Dictionary<string, int>()
{
{ "test1", 33 },
{ "test2", 22 },
{ "test3", 2 },
{ "test4", 1 },
}
},
{
"third", new Dictionary<string, int>()
{
{ "test1", 41 },
{ "test2", 31 },
{ "test3", 12 },
{ "test4", 11 },
}
},
};
我想添加我有键“test1”的所有值。
我试过这样:
int totalTest1 = dic.Sum(x => x.Value.Where(l => l.Key == "test1").SelectMany(z => z.Value));
但是报错
Severity Code Description Project File Line Suppression Status
Error CS0411 The type arguments of the method "Enumerable.SelectMany <TSource, TResult> (IEnumerable <TSource>, Func <TSource, IEnumerable <TResult>>)" cannot be inferred based on usage. Try to explicitly specify the type arguments. CalculadoraLancamento C: \ Users \ sergi \ source \ repos \ CalculadoraLancamento \ CalculadoraLancamento \ Form1.cs 183 Active
您要查找的查询是:
int totalTest1 =
dic
.SelectMany(x => x.Value)
.Where(z => z.Key == "test1")
.Select(z => z.Value)
.Sum();
SelectMany
将外部字典展平为 IEnumerable<KeyValuePair<string, int>>
,这很容易在 Key
上过滤,然后在值上 Sum
上过滤。
根据您提供的示例数据,我得到 95
。
您可能不需要对内部字典执行 LINQ 查询,因为您只是通过键访问字典中的值:
int totalTest1 = dic.Sum(x => x.Value["test1"]);
如果您不确定内部字典是否始终包含指定的键,您可以在访问前检查:
int totalTest1 = dic.Sum(x => x.Value.TryGetValue("test1", out int result) ? result : 0);
获得 95 的另一个变体
Console.WriteLine(dic.SelectMany(x => x.Value)
.Where(x => x.Key == "test1")
.Sum(x => x.Value)); // put criteria into SUM
这实际上比使用 TryGetValue