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?