diff --git a/EntityFrameworkCore.Console/Program.cs b/EntityFrameworkCore.Console/Program.cs index 63590a4..7120de4 100644 --- a/EntityFrameworkCore.Console/Program.cs +++ b/EntityFrameworkCore.Console/Program.cs @@ -15,7 +15,24 @@ context.Database.EnsureCreated(); // Rather than raise: "System.InvalidOperationException: Sequence contains no elements." // var firstCoach = await context.Coaches.FirstOrDefaultAsync(); -await FilterScoredHomeMatches(); +async Task ProjectionAndAnonymousDataTypes() +{ + var teams = await context.Teams + .Select(t => new TeamDetailsDTO + { + TeamId = t.Id, + TeamName = t.Name, + CoachName = t.Coach.Name, + TotalHomeGoals = t.HomeMatches.Sum(m => m.HomeTeamScore), + TotalAwayGoals = t.AwayMatches.Sum(m => m.AwayTeamScore) + }) + .ToListAsync(); + + foreach (var team in teams) + { + Console.WriteLine($"{team.TeamName} - {team.CoachName} | H: {team.TotalHomeGoals} | A: {team.TotalAwayGoals}"); + } +} async Task FilterScoredHomeMatches() { @@ -428,4 +445,13 @@ class TeamInfoDTO { public DateTime Created { get; set; } public string Name { get; set; } +} + +class TeamDetailsDTO +{ + public int TeamId { get; set; } + public string TeamName { get; set; } + public string CoachName { get; set; } + public int TotalHomeGoals { get; set; } + public int TotalAwayGoals { get; set; } } \ No newline at end of file