Created
January 18, 2016 20:13
-
-
Save jd-alexander/53eda4e2b52f84a1274d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This query sorts the users baised on their scores. | |
var userScores = Context.Users.Select(user => new UserScoreModel { | |
Points = user.UserPoints.Where(p => (p.AwardedOn - DateTime.UtcNow).Days < 7).Sum(p => p.Point.Value), | |
Country = user.Country, | |
FacebookId = user.FacebookUserId, | |
Name = user.FirstName + " " + user.LastName, | |
}) | |
.OrderByDescending(user => user.Points) | |
.Select((userScoreModel, rank)=> new UserScoreModel | |
{ | |
Points=userScoreModel.Points, | |
Country = userScoreModel.Country, | |
FacebookId = userScoreModel.FacebookId, | |
Name = userScoreModel.Name, | |
Position=rank+1 | |
}); | |
var leaderboard = new LeaderboardModel(); | |
//So here I am getting the current user that's asking api and setting their current score and rank. | |
leaderboard.CurrentUser=userScores.Where(user=>user.UserId==userId).Select((user,rank) => new UserScoreModel | |
{ | |
Country = user.Country, | |
FacebookId = user.FacebookId, | |
Name = user.Name, | |
Position = rank + 1, | |
Points=user.Points | |
}).FirstOrDefault(); | |
//Here I'm taking the top 20 users which might include current user. | |
leaderboard.Users = userScores.Take(20).ToList(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment