I need calculate the average of a List into a dictionary, I´m start using this syntax to access to the double List and gets the average but the compiler give an error, I´m new working with dictionarys, how is the correct syntax to access each double List and gets the average?
Here is the example of my code:
Dictionary<string, List<double>> SignalValues =
new Dictionary<string, List<double>>();
var dSignalAvg = SignalValues
.Where(x => x.Key == "Key")
.Average(x => x.Value);
Thank you so much!
>Solution :
Like this?
Given a dictionary like this:
Dictionary<string,List<double>> values = new Dictionary<string,List<double>>();
Getting the average for a specific key is easy:
double avgValue = values
.Where( x => x.Key == "Key")
.Select( x => x.Value.Average() )
.FirstOrDefault()
;
Or, if you want to reduce your dictionary to a dictionary of averages, it’s just:
Dictionary<string,double> avgValues = values
.ToDictionary(
x => x.Key ,
x => x.Value.Average()
)
;