linq 实现 sql 中 rank() over(order by score)
有个需求,例如:对学生分数排名,且并列排名
| FinalAchievement(分数) | Ranking排名 |
| 98分 | 1 |
| 97分 | 2 |
| 97分 | 2 |
| 96分 | 4 |
| 96分 | 4 |
sql中是: select Rank Over (order by FinalAchievement desc ) as rank from datas;
)
linq中:
var query = from s in datas
orderby s.FinalAchievement descending
select new
{
Name = s.StudentName,
Ranking = (from o in datas
where float.Parse(o.FinalAchievement) > float.Parse(s.FinalAchievement)
select o).Count() + 1
};
浙公网安备 33010602011771号