[英]LINQ with multiple selection and group by multiple selection
i have a table like this: 我有这样一张桌子:
First Field | Second Field | Third Field | Fourth Field
------------+--------------+----------------+----------------
5 | aaaa | 47 | abc
5 | aaaa | 57 | def
5 | aaaa | 63 | xyz
5 | bbbb | 39 | www
5 | bbbb | 37 | lol
5 | cccc | 84 | yuy
I want to group this table by first and the second field, but i want to get only the rows with the biggest third fields. 我想按第一和第二字段对该表进行分组,但是我只想获取具有最大第三字段的行。
How can i do this via LINQ in c#? 如何在C#中通过LINQ做到这一点?
var curi = (from row in customClassList.AsEnumerable()
group row by new
{
First = row.FirstField.ToString(),
Second = row.SecondSield.ToString(),
} into grp
select new FieldsClass
{
First = grp.Key.First,
Second = grp.Key.Second,
Third = grp.Select(p => p.ThirdField).Max().ToShort(),
Fourth = grp.Key.Fourth
}).ToList();
This is my expected result: 这是我的预期结果:
First Field | Second Field | Third Field | Fourth Field
------------+--------------+----------------+----------------
5 | aaaa | 63 | xyz
5 | bbbb | 39 | www
5 | cccc | 84 | yuy
How about 怎么样
var result = customClassList.GroupBy(x => new {x.FirstField, x.SecondField})
.Select(x => x.OrderByDescending(y => y.ThirdField).First());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.