private List <PlayerMatchStats> GetPlayerMatchStats(int playerId) { var query = (from pss in (from u in this.Context.MatchesLineUp join s in this.Context.MatchesScore on u.MatchId equals s.MatchId join ps in this.Context.PlayersScore on u.Id equals ps.MatchesLineUpId into r from pss in r.DefaultIfEmpty() where u.PlayerId == playerId select new { s.MatchId, s.Match.Year, u.Player.CurrentTeamId, s.TeamId, s.HalfTimeScore, s.FullTimeScore, u.ManOfTheMatch, u.YellowCard, Goals = pss == null ? 0 : 1 }) group pss by new { pss.MatchId, pss.Year, pss.CurrentTeamId, pss.TeamId, pss.HalfTimeScore, pss.FullTimeScore, pss.ManOfTheMatch, pss.YellowCard } into gr select new { Pss = gr.Key, Goals = gr.Sum(x => x.Goals) }).ToList(); var playerMatchStats = query.GroupBy(x => new { x.Pss.MatchId, x.Pss.Year, x.Pss.CurrentTeamId, x.Pss.ManOfTheMatch, x.Pss.YellowCard, x.Goals }, (key, gr) => new PlayerMatchStats { MatchId = key.MatchId, MatchYear = key.Year.ToString(), ManOfTheMatch = key.ManOfTheMatch, YellowCard = key.YellowCard, MatchGoals = gr.Sum(gg => gg.Pss.FullTimeScore), Goals = key.Goals, MatchResult = MatchResultHelper.Result(gr.Select(si => new ScoreInfo { TeamId = si.Pss.TeamId, HalfTimeScore = si.Pss.HalfTimeScore, FullTimeScore = si.Pss.FullTimeScore }).ToList(), key.CurrentTeamId) }).ToList(); return(playerMatchStats); }