LINQ Query with grouping and ranking

前端 未结 5 1690
醉话见心
醉话见心 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:00

    This code seems to be what you need:

        var result = this.DataList
                .GroupBy(data => data.Week)
                .Select(data=>
                {
                    return data.GroupBy(item => item.Name)
                        .Select(item => new { Name = item.Key, SumPoints = item.Sum(v => v.Points) })
                        .OrderBy(item => item.SumPoints)
                        .FirstOrDefault();
                })
                .GroupBy(_=>_.Name)
                .ToDictionary(_=>_.Key, _=>_.Count());
    

提交回复
热议问题