public List <TopTenMatch> GetTopTenMatches(MatchDuration duration) { var filter = Builders <Player> .Filter .Where(x => x.matches.All(m => m.date >= duration.FromDate & m.date <= duration.ToDate)); var filterBson = Builders <BsonDocument> .Filter .Where(x => x["matches"]["date"] >= duration.FromDate& x["matches"]["date"] <= duration.ToDate); var documents = playerList .Aggregate() .Unwind(x => x.matches) .Match(filterBson) .SortByDescending(x => x["matches"]["score"]) .Limit(10) .ToList(); List <TopTenMatch> topTenMatches = new List <TopTenMatch>(); foreach (var item in documents) { TopTenMatch match = new TopTenMatch(); match.score = item["matches"]["score"].ToDouble(); match.level_number = item["matches"]["level_number"].ToInt32(); match.date = item["matches"]["date"].ToUniversalTime(); match.player_nickname = item["nickname"].ToString(); topTenMatches.Add(match); } return(topTenMatches); }
public async Task <ActionResult <IEnumerable <TopTenMatch> > > Put_GetTopTenMatches(int id, MatchDuration duration) { var myMatches = await _context.match.Where(x => x.date >= duration.FromDate& x.date <= duration.ToDate) .OrderByDescending(x => x.score).Take(10).ToListAsync(); if (myMatches == null) { return(NotFound()); } // Create new list with player nick names List <TopTenMatch> returnMatches = new List <TopTenMatch>(); foreach (var match in myMatches) { TopTenMatch newMatch = new TopTenMatch(); var player = await _playerContext.player.FindAsync(match.player_id); newMatch.level_number = match.level_number; newMatch.score = match.score; newMatch.date = match.date; newMatch.player_nickname = player.nickname; returnMatches.Add(newMatch); } return(returnMatches); }