Linq - Top value from each group

前端 未结 6 1809
盖世英雄少女心
盖世英雄少女心 2020-12-03 01:10

How can I employ Linq to select Top value from each group

when I have a code segment like :

var teams = new Team[]
 { 
  new Team{PlayerName=\"Ricky\         


        
6条回答
  •  猫巷女王i
    2020-12-03 01:13

    I would suggest you first implement an extension method on the IEnumerbale class called Top For example:

    IEnumerable Top(this IEnumerable target, Func keySelector, int topCount)
    {
        return target.OrderBy(i => keySelector(i)).Take(topCount);
    }
    

    Then you can write:

    teams.GroupBy(team => team.TeamName).Top(team => team.PlayerScore, 1).

    There might be some slight modifications to make it compile.

提交回复
热议问题