Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use LINQ to find a sum?

Tags:

I have this structure:

private readonly Dictionary<string, Dictionary<string, int>> _storage =     new Dictionary<string, Dictionary<string, int>>(); 

key: Firmware(string): key: Device(string) : value CountOfUsers (int)

I need to get the total of users for each device, but I really don't know how to do it with LINQ. Already tried a lot of variants. Please, help!

For now, I just use a whole function for it

private XlsRow2 GetTotalPerDevice(Dictionary<string, Dictionary<string, int>> storage)     {         XlsRow2 totalPerDeviceRow = new XlsRow2();         totalPerDeviceRow._Name = "Grand Total";         totalPerDeviceRow.UseBorders = true;         foreach (var deviceModel in _allDeviceModels)         {             foreach (var firmware in storage)             {                 foreach (var device in firmware.Value)                 {                     var countOfUsers = 0;                     if (deviceModel == device.Key)                     {                         countOfUsers += device.Value;                          if (!_totalsPerDevice.ContainsKey(deviceModel))                         {                             _totalsPerDevice.Add(deviceModel, countOfUsers);                         }                         else                         {                             _totalsPerDevice[deviceModel] += countOfUsers;                         }                     }                 }             }         }         foreach (var deviceModel in _allDeviceModels)         {             if (_totalsPerDevice.ContainsKey(deviceModel))             {                 totalPerDeviceRow._AddColumn(_totalsPerDevice.First(k => k.Key == deviceModel.ToString()).Value.ToString());             }             else             {                 totalPerDeviceRow._AddColumn("");             }         }         return totalPerDeviceRow;     } 
like image 253
Ekaterina Avatar asked Dec 21 '16 12:12

Ekaterina


People also ask

How do you sum two columns in Linq?

Items select new { Sum(p. Total), Sum(p. Done)};

What is any () in Linq?

The Any operator is used to check whether any element in the sequence or collection satisfy the given condition. If one or more element satisfies the given condition, then it will return true. If any element does not satisfy the given condition, then it will return false.

How do you find the average in LINQ query?

In LINQ, you can find the average of the given sequence by using the Average method. This method returns the average of the elements present in the given sequence or collection. It returns nullable, non-nullable decimal, double, floats, int, etc. values.

Where is IEnumerable?

Where() returns a new IEnumerable . It is a filtered version (a projection) of the original sequence, and original is left unchanged. ToList() returns a new list using the projection.


Video Answer


2 Answers

Something like this for example?

var result = _storage.SelectMany(x => x.Value)     .GroupBy(x => x.Key)     .Select(x => new { Device = x.Key, Total = x.Sum(y => y.Value) }); 
like image 112
MakePeaceGreatAgain Avatar answered Oct 11 '22 15:10

MakePeaceGreatAgain


Since the keys for the data that you would like to aggregate is in the second-level dictionary, a good first step would be to dump all key-value pairs from inner dictionaries into a flat sequence. After that all you need is to aggregate the counts, like this:

var res = _storage     .SelectMany(d => d.Value)     .GroupBy(kvp => kvp.Key)     .ToDictionary(g => g.Key, g => g.Sum(kvp => kvp.Value)); 
like image 38
Sergey Kalinichenko Avatar answered Oct 11 '22 14:10

Sergey Kalinichenko