public IRSimSessionDetailsEntity MapToSimSessionDetailsEntity(SimSessionDetailsDTO source, IRSimSessionDetailsEntity target = null)
        {
            if (source == null)
            {
                return(null);
            }
            if (target == null)
            {
                target = new IRSimSessionDetailsEntity();
            }

            target.IRSubsessionId       = source.IRSubsessionId;
            target.IRTrackId            = source.IRTrackId;
            target.IRSeasonName         = source.IRSeasonName;
            target.IRSeasonYear         = source.IRSeasonYear;
            target.IRSeasonQuarter      = source.IRSeasonQuarter;
            target.IRRaceWeek           = source.IRRaceWeek;
            target.IRSessionId          = source.IRSessionId;
            target.LicenseCategory      = source.LicenseCategory;
            target.SessionName          = source.SessionName;
            target.StartTime            = source.StartTime;
            target.EndTime              = source.EndTime;
            target.CornersPerLap        = source.CornersPerLap;
            target.KmDistPerLap         = source.KmDistPerLap;
            target.MaxWeeks             = source.MaxWeeks;
            target.EventStrengthOfField = source.EventStrengthOfField;
            target.EventAverageLap      = source.EventAverageLap;
            target.EventLapsComplete    = source.EventLapsComplete;
            target.NumCautions          = source.NumCautions;
            target.NumCautionLaps       = source.NumCautionLaps;
            target.NumLeadChanges       = source.NumLeadChanges;
            target.TimeOfDay            = source.TimeOfDay;
            target.DamageModel          = source.DamageModel;

            // Track details
            target.IRTrackId       = source.IRTrackId;
            target.TrackName       = source.TrackName;
            target.ConfigName      = source.ConfigName;
            target.TrackCategoryId = source.TrackCategoryId;
            target.Category        = source.Category;

            // Weather details
            target.WeatherType       = source.WeatherType;
            target.TempUnits         = source.TempUnits;
            target.TempValue         = source.TempValue;
            target.RelHumidity       = source.RelHumidity;
            target.Fog               = source.Fog;
            target.WindDir           = source.WindDir;
            target.WindUnits         = source.WindUnits;
            target.Skies             = source.Skies;
            target.WeatherVarInitial = source.WeatherVarInitial;
            target.WeatherVarOngoing = source.WeatherVarOngoing;
            target.SimStartUTCTime   = source.SimStartUTCTime;
            target.SimStartUTCOffset = source.SimStartUTCOffset;

            // Track state details
            target.LeaveMarbles         = source.LeaveMarbles;
            target.PracticeRubber       = source.PracticeRubber;
            target.QualifyRubber        = source.QualifyRubber;
            target.WarmupRubber         = source.WarmupRubber;
            target.RaceRubber           = source.RaceRubber;
            target.PracticeGripCompound = source.PracticeGripCompound;
            target.QualifyGripCompund   = source.QualifyGripCompund;
            target.WarmupGripCompound   = source.WarmupGripCompound;
            target.RaceGripCompound     = source.RaceGripCompound;

            return(target);
        }
        /// <summary>
        /// Get the results of a single session
        /// If <paramref name="sessionId"/> == 0 the lates session with attached result is used
        /// </summary>
        /// <param name="sessionId">Id of the session</param>
        /// <returns>Convenience DTO for all session results</returns>
        public SessionResultsDTO GetResultsFromSession(long sessionId, bool includeRawResults = false, ScoredResultDataDTO[] scoredResults = null)
        {
            var mapper = new DTOMapper(DbContext);

            // get session entity from database
            SessionBaseEntity session;

            if (sessionId == 0)
            {
                // if sessionId is 0 get latest session with result
                session = DbContext.Set <SessionBaseEntity>()
                          .Where(x => x.SessionResult != null)
                          .OrderByDescending(x => x.Date)
                          .FirstOrDefault();
            }
            else
            {
                // else get session from sessionId
                session = DbContext.Set <SessionBaseEntity>().Find(sessionId);
            }

            if (session == null)
            {
                return(new SessionResultsDTO()
                {
                    SessionId = sessionId
                });
            }
            sessionId = session.SessionId;

            // get session race number
            int raceNr = 0;

            if (session.SessionType == iRLeagueManager.Enums.SessionType.Race)
            {
                var season         = session.Schedule.Season;
                var seasonSessions = season.Schedules.SelectMany(x => x.Sessions).Where(x => x.SessionType == iRLeagueManager.Enums.SessionType.Race).OrderBy(x => x.Date);
                raceNr = (seasonSessions.Select((x, i) => new { number = i + 1, item = x }).FirstOrDefault(x => x.item.SessionId == sessionId)?.number).GetValueOrDefault();
            }

            // get scoredResults using ModelDataProvider
            var                  modelDataProvider = new ModelDataProvider(DbContext);
            ResultDataDTO        rawResults        = null;
            SimSessionDetailsDTO sessionDetails    = null;

            if (session.SessionResult != null)
            {
                if (scoredResults == null)
                {
                    scoredResults = new ScoredResultDataDTO[0];
                    var ids = session.Scorings.Select(x => new KeyValuePair <long, long>(x.ScoringId, session.SessionId));
                    //scoredResults = session.Scorings.Select(x => modelDataProvider.GetScoredResult(sessionId, x.ScoringId)).ToArray();
                    scoredResults = GetScoredResults(ids);
                }

                // get rawResults if includeRawResults == true
                if (includeRawResults)
                {
                    rawResults = mapper.MapToResulDataDTO(session.SessionResult);
                }

                // get session details
                sessionDetails = mapper.MapToSimSessionDetailsDTO(session.SessionResult.IRSimSessionDetails);
            }

            // construct SessionResultsDTO
            var resultsDTO = new SessionResultsDTO()
            {
                Count          = scoredResults.Count(),
                ScheduleId     = session.ScheduleId,
                ScheduleName   = session.Schedule.Name,
                RaceNr         = raceNr,
                RawResults     = rawResults,
                ScoredResults  = scoredResults,
                SessionId      = sessionId,
                SessionDetails = sessionDetails,
                LocationId     = session.LocationId
            };

            return(resultsDTO);
        }