I have a dictionary and I am trying to add the value that is inside another dictionary.
Below I have the 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 },
}
},
};
I want to add all the values where I have the key "test1".
I've tried it this way:
int totalTest1 = dic.Sum(x => x.Value.Where(l => l.Key == "test1").SelectMany(z => z.Value));
But it is giving error
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