0

I'm trying to build a leaderboards based on an int score. I'm pulling the records out and calculating the rank like so:

//Base query
var query = UoW.Repository<UserProfile>().Get().OrderByDescending(u => u.Points);

//Fetch the data including a groupby count
var data = await query
        .Skip(skip)
        .Take(pageSize)
        .Select(u => new UserListItem
        {
             Points = u.Points.ToString(),
             Username = u.DisplayName,
             Rank = query.Count(o => o.Points > u.Points) + 1 //Calculates the rank (index)
        })
        .GroupBy(u => new { Total = query.Count() })
        .FirstOrDefaultAsync();

This works fine except when I have 2 or more values with the same points it duplicate the position. I need it to be continuously incremented regardless of ties. This is how is currently displays the results:

Rank | User | Points

  • 1 | User 1 | 456
  • 2 | User 2 | 420
  • 3 | User 3 | 402
  • 4 | User 4 | 380
  • 4 | User 5 | 380

Any idea how I can get it to auto increment correctly?

Luke Stoward
  • 1,480
  • 14
  • 24

1 Answers1

0

The solution is to add more conditions to the Rank function, to determine which comes first when the scores are tied. It could even be ranking by username a- > z if you like.

Answers to point you in the right direction to use multiple criteria can be found in this SO answer.

Community
  • 1
  • 1
MeanGreen
  • 3,098
  • 5
  • 37
  • 63