Converting SQL Rank() to LINQ, or alternative

前端 未结 4 1982
野趣味
野趣味 2020-12-06 06:00

I have the below SQL statement that works as desired/expected. However I would like to translate it into a LINQ statement(Lambda??) so that it will fit with the rest of my

4条回答
  •  自闭症患者
    2020-12-06 06:16

    Based on answer from @Totero but with a lamda implementation. Higher score = higher rank.

    var rankedData = data.Select(s => new{
                        Ranking = data.Count(x => x.Value > s.Value)+1,
                        Name = s.Key,
                        Score = s.Value});
    

    For this input:

    { 100, 100, 98, 97, 97, 97, 91, 50 }
    

    You will get this output:

    • Score : Rank
    • 100 : 1
    • 100 : 1
    • 98 : 3
    • 97 : 4
    • 97 : 4
    • 97 : 4
    • 91 : 6
    • 50 : 7

提交回复
热议问题