I struggle to get a ranking place of a user in a list.
My list looks like this:
User | Score |
---|---|
1 | 10 |
2 | 10 |
1 | 5 |
3 | 5 |
2 | 40 |
1 | 10 |
I try to get the ranking of user 3.
To do it I do a first linq request with group by user id.
And then I do a request to sum the scores(nbpoints) and orderby descending like this I have:
User 2 with 50 points User 1 with 25 points User 3 with 5 points
Then how can I get the ranking of the user? I tried the index with the select but it takes the index before the sorting.
My request is below:
MyScoreRankObject = users.Select((u, index) =>
{
return new StatResultDto
{
ActualScore = u.Sum(s => s.NbPoints),
Index = u.Index
};
}).OrderByDescending(s => s.ActualScore).Where(u => u.Id ==3).FirstOrDefault();
How can I do it to get the raniking?
I try to get:
Request for user 3 gives me the object:
new StatResultDto
{
ActualScore = u.Sum(s => s.NbPoints),
Index = u.Index // Gives the ranking
};
Thanks,