LINQ Query with grouping and ranking

前端 未结 5 1738
醉话见心
醉话见心 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 20:59

    It was somewhat confusing to see that your query didn't seem to correspond to the data. So instead, this will be based on the data alone. The query should produce valid SQL so you won't have to use LINQ to Objects. You can adapt it to your tables with little modification.

    var query = from pred in Predictions
                group pred.Points by pred.WeekNum into week
                join pred in Predictions
                    on new { WeekNum = week.Key, Points = week.Max() }
                    equals new { pred.WeekNum, pred.Points }
                group 1 by pred.Player into player
                let Wins = player.Count()
                orderby Wins descending, player.Key
                select new
                {
                    Player = player.Key,
                    Wins,
                };
    

提交回复
热议问题