C# 获取不同的最大值

标签 c# linq max distinct

这是我的数据表

X       Y   P
Str1    C1  10
Str1    C1  5
Str1    C1  1
Str1    C1  2
Str2    C1  25
Str2    C1  4
Str1    C2  411
Str1    C2  2356
Str2    C2  12
Str2    C2  33

我正在尝试根据 X 和 Y 列获取不同的行,并使用 linq to dataset 获取 P 列的最大值

 X      Y   P
Str1    C1  10
Str2    C1  25
Str1    C2  2356
Str2    C2  33

-

            var query0 = (from row in tablo.AsEnumerable() 

        select new
        {
            X = row.Field<string>("X"),
            Y = row.Field<string>("Y"),

        }).Distinct();

我该怎么办? 谢谢。

最佳答案

你需要 GroupBy 在这里:-

var result = tablo.AsEnumerable().
                  .GroupBy(row => new 
                            { 
                               X= row.Field<string>("X"), 
                               Y = row.Field<string>("Y") 
                            })
                  .Select(x => new 
                              {
                                 X = x.Key.X,
                                 Y = x.Key.Y,
                                 P = x.Max(z => z.Field<int>("P"))
                              });

关于C# 获取不同的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34707180/

相关文章:

c# - 使用命令式绑定(bind)时进行 Azure Functions 测试

c# - 无法将 lambda 表达式转换为类型 'object',因为它不是委托(delegate)类型

r - 使用 'summarise' 的数据帧列的第二(或第三)最大值

c - 在 C 中寻找函数的局部最大值的问题

c# - 为什么集合初始值设定项不能与表达式主体属性一起使用?

c# - 我应该为动态报告生成器使用哪种设计模式?

c# - 使用 linq 的 C# windows 服务出现问题

sql - 在 Postgres 中使用 max 和 group by 获取第二个属性

c# - 如何从此代码中隐藏 CMD 控制台?

c# - 向用户呈现文件的 XML 值的最佳方式?