使用 LINQ 对数据列表进行分组并从每个分组数据中获取最大值
Grouping list of data with LINQ and get maximum from each grouped data
我有一个class,假设如下:
public class Customer{
public string Name;
public int Score;
}
所以,我有一份客户名单。客户可能具有相同的名称和不同的分数。现在我想在名字相同的情况下获得最高分的客户。
例如:
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 },
};
输出应该是,
Rahim 30
Karim 49
Aziz 24
使用 linq,我们需要先按名称分组,然后应用 SelectMany 从每个组中检索最高分,如下所示:
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;
}
使用以下查询:
var customers = customerList
.GroupBy(x => x.Name)
.Select(g => g.OrderByDescending(x => x.Score).First())
.ToList();
使用 MaxBy
运算符非常简单:
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 },
};
使用 .NET 6.0:
List<Customer> output =
customers
.GroupBy(c => c.Name)
.Select(c => c.MaxBy(x => x.Score))
.ToList();
使用微软的交互框架(System.Interactive
):
List<Customer> output =
customers
.GroupBy(c => c.Name)
.SelectMany(c => c.MaxBy(x => x.Score))
.ToList();
这给了我:
我有一个class,假设如下:
public class Customer{
public string Name;
public int Score;
}
所以,我有一份客户名单。客户可能具有相同的名称和不同的分数。现在我想在名字相同的情况下获得最高分的客户。
例如:
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 },
};
输出应该是,
Rahim 30
Karim 49
Aziz 24
使用 linq,我们需要先按名称分组,然后应用 SelectMany 从每个组中检索最高分,如下所示:
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;
}
使用以下查询:
var customers = customerList
.GroupBy(x => x.Name)
.Select(g => g.OrderByDescending(x => x.Score).First())
.ToList();
使用 MaxBy
运算符非常简单:
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 },
};
使用 .NET 6.0:
List<Customer> output =
customers
.GroupBy(c => c.Name)
.Select(c => c.MaxBy(x => x.Score))
.ToList();
使用微软的交互框架(System.Interactive
):
List<Customer> output =
customers
.GroupBy(c => c.Name)
.SelectMany(c => c.MaxBy(x => x.Score))
.ToList();
这给了我: