LINQ Query with grouping and ranking

前端 未结 5 1686
醉话见心
醉话见心 2021-01-13 20:32

I have a sql table called predictions with data as below

Week    Player     Points
201101  Mark       7
201101  Mark       7
201101  Pete       7
201101  Pet         


        
5条回答
  •  北海茫月
    2021-01-13 21:04

    mine is quite long

    var weeklyTopScore = from eachMatch in FootballPredictions
                         group eachMatch by eachMatch.week
                         into weekly
                         select new {week = weekly.Key, topScore = weekly.Max(match => match.points)};
    
    var playerWins = from eachResult in weeklyTopScore
                     join entry in FootballPredictions
                     on eachResult.week equals entry.week
                     where eachResult.topScore == entry.points
                     group entry by entry.player
                     into winner
                     select new { player = winner.Key, wins = winner.Count() };
    
    var result = from entry in playerWins
                 group entry by entry.wins
                 into summary
                 select new { player = summary.Select(data => data.player).Aggregate((cur, nex) => cur + ", " + nex), wins = summary.Key};
    

提交回复
热议问题