Home > OS >  Gouping list of data with linq and get maximum from each grouped data
Gouping list of data with linq and get maximum from each grouped data

Time:03-24

I have a class, let's say as below:

  public class Customer{
      public string Name;
      public int Score;
  }

So, I have a list of customers. Customers may have same name with different Scores. Now I want to get the customer with the maximum Score if the name is same.

For example:

var customers = new List<Customer>()
{
    new Customer() { Name = "Rahim", Score = 30 },
    new Customer() { Name = "Rahim", Score = 25 },
    new Customer() { Name = "Karim", Score = 49 },
    new Customer() { Name = "Aziz", Score = 24 },
};

The output should be,

Rahim 30
Karim 49
Aziz 24

CodePudding user response:

Use the following query:

var customers = customerList
    .GroupBy(x => x.Name)
    .Select(g => g.OrderByDescending(x => x.Score).First())
    .ToList();

CodePudding user response:

It's nice and simple with the MaxBy operator:

var customers = new List<Customer>()
{
    new Customer() { Name = "Rahim", Score = 30 },
    new Customer() { Name = "Rahim", Score = 25 },
    new Customer() { Name = "Karim", Score = 49 },
    new Customer() { Name = "Aziz", Score = 24 },
};

Using .NET 6.0:

List<Customer> output =
    customers
        .GroupBy(c => c.Name)
        .Select(c => c.MaxBy(x => x.Score))
        .ToList();

Using Microsoft's Interactive Framework (System.Interactive):

List<Customer> output =
    customers
        .GroupBy(c => c.Name)
        .SelectMany(c => c.MaxBy(x => x.Score))
        .ToList();

That gives me:

output

CodePudding user response:

Using linq, we need to group by name first and then apply SelectMany to retrieve the highest score from each group as below:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    class HelloWorld {
      static void Main() {   
          var customerList = new List<Customer>{
              new Customer {Name = "a", Score =30},
               new Customer {Name = "a", Score =35},
                new Customer {Name = "b", Score =20},
                 new Customer {Name = "c", Score =50},
                  new Customer {Name = "b", Score =60},
                   new Customer {Name = "a", Score =80},
          };

    List<Customer> customers = customerList
    .GroupBy(t => t.Name)
    .SelectMany(a => a.Where(b => b.Score == a.Max(c => c.Score))).ToList();
                
                foreach(var data in customers){
                    Console.WriteLine(data.Name  ","  data.Score);
                }   
                Console.ReadLine();
      }
      
      public class Customer{
          public string Name;
          public int Score;
      }
  • Related