2021年3月6日星期六

linq issue to get a ranking

I struggle to get a ranking place of 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 groupby 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 hoiw 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,

https://stackoverflow.com/questions/66512602/linq-issue-to-get-a-ranking March 07, 2021 at 10:13AM

没有评论:

发表评论