使用 Lambda 表达式从多个记录中获取最大值

Get maximal values from several records with Lambda expression

public class MySoft
{
    public string SoftName { get; set; }
    public int Version { get; set; }
    public decimal Price { get; set; }

}

作为数据,我有 "MySoft"(软件名称、版本、价格)列表:

SoftA   1   25
SoftB   1   35
SoftB   2   12
SoftB   3   24
SoftA   2   14

我希望保留最高版本的名称、价格和版本。

结果应该是:

SoftA 2 14
SoftB 3 24

你有什么想法吗?

谢谢,

只需按名称分组,然后在版本上对每个组进行排序并取最后一个。

var results = list.GroupBy(x => x.SoftName)
    .Select(g => g.OrderBy(x => x.Version).Last());

你也可以这样做,

 var result = softwares.GroupBy(item => item.SoftName)
        .Select(grp => grp.Aggregate((max, cur) =>
                             (max == null || cur.Version > max.Version) ? cur : max));

Fiddle