在linq中获得每组最高排名的项目 [英] Get the highest ranked items per group in linq
本文介绍了在linq中获得每组最高排名的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要选择每个属性排名最高的项目
我在linq中的功能排名如下:
I need to select items with the highest rank per attribute
I have a functional ranking in linq that looks like this:
MyCollection.GroupBy(c => c.Attribute).Select(g => g.OrderByDescending(c => c.Prio).First())
它工作正常,除非我在排名中获得关联,我只得到其中一个绑定的项目。 />
有谁知道如何获得所有优先级最高的并列项目?
什么我试过了:
And it works fine except when I get ties in the ranking I only get one of the tied items.
Does anyone know how to get all of the tied items with the highest priority?
What I have tried:
MyCollection.GroupBy(c => c.Attribute).Select(g => g.OrderByDescending(c => c.Prio).First())
推荐答案
你不能自动完成,因为你已经非常明确地告诉它只返回一个元素每个小组 - 这就是First做的!
试试这个:
You can't do it automatically, because you have very specifically told it to only return one element from each group - that's what First does!
Try this:
MyCollection.GroupBy(c => c.Attribute).SelectMany(g => g.Where(p => p.Prio == g.Max(h => h.Prio)))
这篇关于在linq中获得每组最高排名的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文