private void SelectedPersonChanged(int shooterId) { if (MessengerInstance == null) { return; } Dictionary <int, List <int> > sessionToSubsession = (from session in _sessionDataStore.FindByShooterId(shooterId) join subsession in _sessionSubtotalDataStore.GetAll() on session.SessionId equals subsession.SessionId orderby session.ProgramNumber group subsession by session.SessionId).ToDictionary(_ => _.Key, _ => _.Select(fo => fo.SessionSubtotalId).ToList()); Sessions = new ObservableCollection <SessionViewModel>(); foreach (KeyValuePair <int, List <int> > keyValuePair in sessionToSubsession) { Session session = _sessionDataStore.FindById(keyValuePair.Key); { ParticipationDescription participation = _sdk.ParticipationDescriptions.GetAll() .SingleOrDefault(x => x.ProgramNumber == session.ProgramNumber.ToString()); string programName = participation == null ? "unknown" : participation.ProgramName; SessionViewModel svm = new SessionViewModel { LaneNumber = session.LaneNumber, ProgramName = string.Format("{0} [{1}]", programName, session.ProgramNumber), SessionId = session.SessionId, ShooterIsParticipating = _shooterParticipationDataStore.FindByShooterId(shooterId) .Any(x => x.ProgramNumber == session.ProgramNumber) }; List <Shot> shots = new List <Shot>(); foreach (int subsessionId in keyValuePair.Value) { shots.AddRange(_shotDataStore.FindBySubSessionId(subsessionId).OrderBy(shot => shot.Ordinal)); } svm.Shots = new ObservableCollection <Shot>(shots); svm.Total = shots.Sum(s => s.PrimaryScore); Sessions.Add(svm); } } }
private IEnumerable <Tuple <Person, int, decimal, decimal> > GetBestResults(int programNumber) { IShooterDataStore shooterDataStore = ServiceLocator.Current.GetInstance <IShooterDataStore>(); IShooterParticipationDataStore shooterParticipationDataStore = ServiceLocator.Current.GetInstance <IShooterParticipationDataStore>(); ISessionDataStore sessionDataStore = ServiceLocator.Current.GetInstance <ISessionDataStore>(); ISessionSubtotalDataStore sessionSubtotalDataStore = ServiceLocator.Current.GetInstance <ISessionSubtotalDataStore>(); IShotDataStore shotDataStore = ServiceLocator.Current.GetInstance <IShotDataStore>(); IPersonDataStore personDataStore = ServiceLocator.Current.GetInstance <IPersonDataStore>(); List <Shooter> shooters = (from s in shooterDataStore.GetAll() join sp in shooterParticipationDataStore.GetAll() on s.ShooterId equals sp.ShooterId where sp.ProgramNumber == programNumber select s).ToList(); // Person, ShooterId, TotalScore, DeepShot List <Tuple <Person, int, decimal, decimal> > result = new List <Tuple <Person, int, decimal, decimal> >(); foreach (Shooter shooter in shooters) { // Get the relevant sessions var lShooter = shooter; IEnumerable <Session> sessions = from s in sessionDataStore.GetAll() where s.ShooterId == lShooter.ShooterId && s.ProgramNumber == programNumber select s; // Get the results to the sessions var maxScoreGrouping = from session in sessions join subTotal in sessionSubtotalDataStore.GetAll() on session.SessionId equals subTotal.SessionId join shot in shotDataStore.GetAll() on subTotal.SessionSubtotalId equals shot.SubtotalId group shot by session into grouping select new { Session = grouping.Key, TotalScore = grouping.Sum(x => x.PrimaryScore) }; // Get the max scores of each session var bestGrouping = maxScoreGrouping.OrderByDescending(x => x.TotalScore).FirstOrDefault(); if (bestGrouping != null) { // select the max score as the result which shall go into ranking decimal maxScore = bestGrouping.TotalScore; decimal?deepShot = (from subTotal in sessionSubtotalDataStore.GetAll() join shot in shotDataStore.GetAll() on subTotal.SessionSubtotalId equals shot.SubtotalId where subTotal.SessionId == bestGrouping.Session.SessionId orderby shot.SecondaryScore descending select shot.SecondaryScore).FirstOrDefault(); if (lShooter.PersonId.HasValue) { Person person = personDataStore.FindById(lShooter.PersonId.Value); result.Add(new Tuple <Person, int, decimal, decimal>(person, shooter.ShooterId, maxScore, deepShot ?? 0)); } } } return(result); }