如何按条件获取组的最大对象(整个对象,而不是它的属性)

时间:2013-02-10 17:28:40

标签: c# linq

我希望通过某些标准获得IGrouping的最大对象,但到目前为止,我似乎找不到允许我在组内返回整个对象的重载

List<Player> players = new List<Player>() 
{ 
     new Player() { Name = "Peyton Manning", Age = 36, Position = "QB", Salary = 19000000 },
     new Player() { Name = "Tom Brady", Age = 35, Position = "QB", Salary = 18400000 },
     new Player() { Name = "Drew Brees", Age = 34, Position = "QB", Salary = 21000000 },
     new Player() { Name = "Randy Moss", Age = 35, Position = "WR", Salary = 7000000 },
     new Player() { Name = "Marvin Harrison", Age = 38, Position = "WR", Salary = 11000000 },
     new Player() { Name = "Demaryius Thomas", Age = 23, Position = "WR", Salary = 5000000 },
     new Player() { Name = "Ryan Clady", Age = 26, Position = "OT", Salary = 10000000 },
};

我想写一个返回结果集的查询,我可以像这样枚举:

foreach(Player player in highestPaidPlayers) 
{
      Console.WriteLine("The highest paid {0} is {1}, who is being paid {2}", player.Position, player.Name, player.Salary);
}

到目前为止,这是我得到的:

var playersGroupedByPosition = players
    .GroupBy(p => p.Position)
    .Select(g => g.Max(p => p.Salary));

但这只会给我一份最高薪水清单(例如整数清单)。 Max允许我从Player以外的IGrouping返回Max()对象,而IComparable依赖于一个对象来实现MaxBy {{1}}。 1}}。

我的LINQ有点弱,所以我还没弄明白如何编写一个子查询来获取组的最大条目并将其返回到外部选择,但有没有更直接的方法呢?

我见过使用第三方库MoreLINQ(及其自定义运算符{{1}})引用的解决方案但我不想去那条路(除非无法做到)使用现有的查询运算符集 - 如果没有,我想了解现有运算符的限制,以防止在查询中执行此操作。

2 个答案:

答案 0 :(得分:4)

我认为使用ToListOrderBy可以避免使用第三方代码:

IEnumerable<Player> highestPaidPlayers = players.GroupBy(p => p.Position)
    .Select(g => g.ToList().OrderBy(p => p.Salary).Last());

答案 1 :(得分:2)

使用嵌套“subselect”的解决方案

表达式语法解决方案:

var highestPaidPlayers = players.GroupBy(p => p.Position)
                                 .Select(x => x.Where(p => p.Salary == x.Max(m => m.Salary)).First());

查询语法解决方案:

var highestPaidPlayers = from pl in players
                         group pl by pl.Position
                         into g
                             let max = g.Max(x => x.Salary)
                             let maxPl = g.Where(x => x.Salary == max).First()
                             select maxPl;

试试Ideone