BusinessLogic.Logic.PlayedGames.PlayedGameRetriever.GetRecentGames C# (CSharp) Method

GetRecentGames() public method

public GetRecentGames ( int numberOfGames, int gamingGroupId, IDateRangeFilter dateRangeFilter = null ) : List
numberOfGames int
gamingGroupId int
dateRangeFilter IDateRangeFilter
return List
        public List<PlayedGame> GetRecentGames(int numberOfGames, int gamingGroupId, IDateRangeFilter dateRangeFilter = null)
        {
            if(dateRangeFilter == null)
            {
                dateRangeFilter = new BasicDateRangeFilter();
            }

            List<PlayedGame> playedGames = dataContext.GetQueryable<PlayedGame>()
                .Where(game => game.GamingGroupId == gamingGroupId
                            && game.DatePlayed >= dateRangeFilter.FromDate
                                              && game.DatePlayed <= dateRangeFilter.ToDate)
                .Include(playedGame => playedGame.GameDefinition.BoardGameGeekGameDefinition)
                .Include(playedGame => playedGame.GamingGroup)
                .Include(playedGame => playedGame.PlayerGameResults
                    .Select(playerGameResult => playerGameResult.Player))
                    .OrderByDescending(orderBy => orderBy.DatePlayed)
                    .ThenByDescending(orderBy => orderBy.DateCreated)
                .Take(numberOfGames)
                .ToList();

            //TODO this seems ridiculous but I can't see how to order a related entity in Entity Framework :(
            foreach (PlayedGame playedGame in playedGames)
            {
                playedGame.PlayerGameResults = playedGame.PlayerGameResults.OrderBy(orderBy => orderBy.GameRank).ToList();
            }

            return playedGames;
        }

Usage Example

        public void ItEagerlyFetchesPlayerGameResults()
        {
            using(NemeStatsDbContext dbContext = new NemeStatsDbContext())
            {
                dbContext.Configuration.LazyLoadingEnabled = false;
                dbContext.Configuration.ProxyCreationEnabled = false;
                using (NemeStatsDataContext dataContext = new NemeStatsDataContext(dbContext, securedEntityValidatorFactory))
                {
                    PlayedGameRetriever retriever = new PlayedGameRetriever(dataContext);

                    List<PlayedGame> playedGames = retriever.GetRecentGames(1, testUserWithDefaultGamingGroup.CurrentGamingGroupId);
                    ICollection<PlayerGameResult> playerGameResults = playedGames[0].PlayerGameResults;

                    Assert.NotNull(playerGameResults);
                }
            }
        }
All Usage Examples Of BusinessLogic.Logic.PlayedGames.PlayedGameRetriever::GetRecentGames