C# 实现排名后的列表跳过数字
我正在尝试实现以下排名功能:C# 实现排名后的列表跳过数字,c#,asp.net,linq,C#,Asp.net,Linq,我正在尝试实现以下排名功能: Name Points rank ram 9 1 kamal 9 1 preet 8 2 lucky 7 3 kishan 6.5 4 devansh 6 5 neha 6 5 我使用了以下代码来实现这一点: finalR
Name Points rank
ram 9 1
kamal 9 1
preet 8 2
lucky 7 3
kishan 6.5 4
devansh 6 5
neha 6 5
我使用了以下代码来实现这一点:
finalResult = finalResult.OrderByDescending(i => i.points).ThenBy(i => i.academy).ToList();
finalResult = finalResult.AsEnumerable() // Client-side from here on
.Select((player, index) => new RankingEntity()
{
competitorid = player.competitorid,
firstname = player.firstname,
lastname = player.lastname,
academy = player.academy,
points = player.points,
place = player.place,
eventId = player.eventId,
eventname = player.eventname,
categoryname = player.categoryname,
Rank = index + 1
}).ToList();
var t = (from i in finalResult
let rank = finalResult.First(x => x.points == i.points)
select new
{
Col1 = i,
Rank = rank.Rank
}).ToList();
List<RankingEntity> ttt = new List<RankingEntity>();
foreach (var item in t)
{
var a = item.Col1;
var row = new RankingEntity();
row.competitorid = a.competitorid;
row.firstname = a.firstname;
row.lastname = a.lastname;
row.academy = a.academy;
row.points = a.points;
row.place = a.place;
row.eventId = a.eventId;
row.eventname = a.eventname;
row.categoryname = a.categoryname;
row.Rank = item.Rank;
ttt.Add(row);
}
finalResult=finalResult.OrderByDescending(i=>i.points).ThenBy(i=>i.academy.ToList();
finalResult=finalResult.AsEnumerable()//从此以后的客户端
.选择((玩家,索引)=>new RankingEntity()
{
competitorid=player.competitorid,
firstname=player.firstname,
lastname=player.lastname,
academy=player.academy,
点数=玩家点数,
place=player.place,
eventId=player.eventId,
eventname=player.eventname,
categoryname=player.categoryname,
排名=指数+1
}).ToList();
变量t=(来自最终结果中的i
让rank=finalResult.First(x=>x.points==i.points)
选择新的
{
Col1=i,
排名
}).ToList();
List ttt=新列表();
foreach(t中的var项目)
{
var a=项目1.Col1;
var row=新的RankingEntity();
row.competitorid=a.competitorid;
row.firstname=a.firstname;
row.lastname=a.lastname;
row.academy=a.academy;
row.points=a.points;
row.place=a.place;
row.eventId=a.eventId;
row.eventname=a.eventname;
row.categoryname=a.categoryname;
行.秩=项.秩;
ttt.添加(行);
}
我得到的结果如下:
请帮助我,我做错了什么 您试图实现的是对“组”的排名,因此,请按分数对结果进行分组,然后对组进行排序。对于组中的每个项目,给出相同的排名
finalResult.GroupBy(item => item.Points) // Group by points
.OrderDescendingBy(g => g.Key) // Order the groups
.Select((g, index) => new { Data = g, GroupRank = index + 1}) // Rank each group
.SelectMany(g => g.Data.Select(item => new RankingEntity
{
/* properties of each item */
Rank = g.GroupIndex
}); // Flatten groups and set for each item the group's ranking
你的方法中的问题是,你给出的是单个项目的排名,而不是组。然后,当您检索组的排名时(from i in finalResult让rank=finalResult.First(x=>x.points==i.points)…
),您实际上为组中的每个项目设置了其中一个元素的排名。因此,如果您首先得到组中的最后一个项目,那么这将是其中每个项目的排名值
还要注意,在代码的第一行中,您使用了
ToList
。因此,不需要在它下面的行中使用AsEnumerable
,它已经是一个物化内存集合。好的,有什么问题吗?看起来排名是正确的。如果你检查一下,截图,16分之后是19分。。不是17岁