Example of LINQ data grouping



  • In the data set, we need to get a summary of the field divided by group.

    Example:

    IEnumerable<Issue>
    Member Issue.Member
    decimal Issue.hours
    

    We have to go through Issue, group the data for the member and calculate the total hours for each group.

    Tried as follows:

    issues
    .Where(w => w.Member != null)
    .Group(g => g.Member)
    .Select(s => {
     s.Key
     // Здесь могу получить только ключ, а как получить данные и дальше их начать суммировать?
    });
    

    At the end of the sample, a pair of values shall be maintained.



  • https://msdn.microsoft.com/ru-ru/library/bb344977(v=vs.110).aspx Maintains its tying, so you can just trigger a method. https://msdn.microsoft.com/ru-ru/library/bb549046(v=vs.110).aspx :

    var data = issues
        .Where(m => m != null)
        .Group(g => g.Member)
        .Select(s => new { s.Key, Sum = s.Sum(i => i.hours) });
    

    // или .Select(s => Tuple.Create(s.Key, s.Sum(i => i.hours)));

    data will contain a list of objects with a core grouping and the value of the sum for each group.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2