gpt4 book ai didi

c# - LINQ GroupBy 并选择不同的属性

转载 作者:太空宇宙 更新时间:2023-11-03 17:32:40 25 4
gpt4 key购买 nike

如果我有以下集合:

var foos = new List<Foo>
{
new Foo{ Name = "A", Value = 1 },
new Foo{ Name = "B", Value = 1 },
new Foo{ Name = "B", Value = 2 },
new Foo{ Name = "C", Value = 1 },
};

最后我想:

A-1
B-2
C-1

在重复“B”的情况下,我想在哪里选择具有最高值的“B”?

类似于:

var filteredFoos = foos.GroupBy(x => x.Name).Select_Duplicate_With_Highest_Value

最佳答案

var query = from p in foos
group p by p.Name into g
select new
{
Name = g.Key,
Value = g.Max(a => a.Value)
};

关于c# - LINQ GroupBy 并选择不同的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14200307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com