LINQ - 选择每组最大属性值的记录 [英] LINQ - Select Records with Max Property Value per Group
问题描述
我有这样的数据集:
GroupName GroupValue MemberName MemberValue
'Group1'1' Member1'1
'Group1'1'Member2'2
'Group2'2'Member3'3
'Group2'2'Member4'2
'Group3'2'Member5' 4
'Group3'2'Member6'1
我想要选择的是行最高 MemberValue
每个 GroupName
,但只限于 GroupName
具有最大的 GroupValue
,并将它们传递给委托函数。像这样:
'Group2'2'Member3'3
'Group3'2'Member5'4
到目前为止,我已经尝试过这种格式...
data.Where(maxGroupValue =>
maxGroupValue.GroupValue == data.Max(groupValue => groupValue.GroupValue))
.Select(FunctionThatTakesData)
...但这只是给我Group2和Group3的每个成员。我已经尝试在 Select()
之前放置 GroupBy()
,但是将输出转换为 IGrouping< string,DataType>
so FunctionThatTakesData()
不知道该如何处理它,我不能做另一个 Where()
过滤掉最大的 MemberValue
s。
我能做些什么来正确过滤这些数据并将其传递到我的函数中? 你可以做到这一点与以下Linq。
var results = data.GroupBy(r = r.GroupValue)
.OrderByDescending(g => g.Key)
.FirstOrDefault()
?.GroupBy(r => r.GroupName)
.Select(g => g.OrderByDescending(r => r.MemberValue)。首先());
首先,您必须将 GroupValue
然后按照 Key
(它是 GroupValue
)的降序对组进行排序并取第一个。现在您拥有所有具有最大值 GroupValue
的行。然后,将 GroupName
中的那些进行分组,并从这些组中按降序对 MemberValue
进行排序,并将首先
行以获取每个 GroupName
组中的行,并使用max MemberValue
来获取行。另外,我还在 FirstOrDefault
之后使用了C#6空条件运算符?。
是空的。如果你不使用C#6,那么你需要事先处理这种情况,你可以使用 First
代替。
I've got a data set like this:
GroupName GroupValue MemberName MemberValue
'Group1' 1 'Member1' 1
'Group1' 1 'Member2' 2
'Group2' 2 'Member3' 3
'Group2' 2 'Member4' 2
'Group3' 2 'Member5' 4
'Group3' 2 'Member6' 1
What I want to select is the rows that have the maximum MemberValue
per GroupName
, but only for those GroupName
s that have the largest GroupValue
, and pass them into a delegate function. Like this:
'Group2' 2 'Member3' 3
'Group3' 2 'Member5' 4
So far I've tried this format...
data.Where(maxGroupValue =>
maxGroupValue.GroupValue == data.Max(groupValue => groupValue.GroupValue))
.Select(FunctionThatTakesData)
...but that just gives me every member of Group2 and Group3. I've tried putting a GroupBy()
before the Select()
, but that turns the output into an IGrouping<string, DataType>
so FunctionThatTakesData()
doesn't know what to do with it, and I can't do another Where()
to filter out only the maximum MemberValue
s.
What can I do to get this data set properly filtered and passed into my function?
You can do that with the following Linq.
var results = data.GroupBy(r = r.GroupValue)
.OrderByDescending(g => g.Key)
.FirstOrDefault()
?.GroupBy(r => r.GroupName)
.Select(g => g.OrderByDescending(r => r.MemberValue).First());
First you have to group on the GroupValue
then order the groups in descending order by the Key
(which is the GroupValue
) and take the first one. Now you have all the rows with the max GroupValue
. Then you group those on the GroupName
and from those groups order the MemberValue
in descending order and take the First
row to get the row in each GroupName
group with the max MemberValue
. Also I'm using the C# 6 null conditional operator ?.
after FirstOrDefault
in case data
is empty. If you're not using C# 6 then you'll need to handle that case up front and you can just use First
instead.
这篇关于LINQ - 选择每组最大属性值的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!