Add example filter function and insert matches.
This commit is contained in:
parent
3652a69730
commit
fc9d89ae97
@ -15,7 +15,67 @@ context.Database.EnsureCreated();
|
||||
// Rather than raise: "System.InvalidOperationException: Sequence contains no elements."
|
||||
// var firstCoach = await context.Coaches.FirstOrDefaultAsync();
|
||||
|
||||
await ExplicitLoadingExample();
|
||||
await FilterScoredHomeMatches();
|
||||
|
||||
async Task FilterScoredHomeMatches()
|
||||
{
|
||||
// Get teams where they scored on home matches.
|
||||
var teams = await context.Teams
|
||||
.Include(t => t.Coach)
|
||||
.Include(t => t.HomeMatches.Where(m => m.HomeTeamScore > 0))
|
||||
.ToListAsync();
|
||||
foreach (var team in teams)
|
||||
{
|
||||
Console.WriteLine($"{team.Name} - {team.Coach.Name}");
|
||||
foreach (var match in team.HomeMatches)
|
||||
{
|
||||
Console.WriteLine($"Score: {match.HomeTeamScore}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async Task InsertMatches()
|
||||
{
|
||||
var match1 = new Match
|
||||
{
|
||||
AwayTeamId = 10,
|
||||
HomeTeamId = 12,
|
||||
HomeTeamScore = 1,
|
||||
AwayTeamScore = 0,
|
||||
Date = new DateTime(2025, 6, 10),
|
||||
TicketPrice = 20
|
||||
};
|
||||
var match2 = new Match
|
||||
{
|
||||
AwayTeamId = 6,
|
||||
HomeTeamId = 9,
|
||||
HomeTeamScore = 1,
|
||||
AwayTeamScore = 0,
|
||||
Date = new DateTime(2025, 6, 11),
|
||||
TicketPrice = 20
|
||||
};
|
||||
var match3 = new Match
|
||||
{
|
||||
AwayTeamId = 13,
|
||||
HomeTeamId = 15,
|
||||
HomeTeamScore = 1,
|
||||
AwayTeamScore = 0,
|
||||
Date = new DateTime(2025, 6, 12),
|
||||
TicketPrice = 20
|
||||
};
|
||||
var match4 = new Match
|
||||
{
|
||||
AwayTeamId = 4,
|
||||
HomeTeamId = 11,
|
||||
HomeTeamScore = 0,
|
||||
AwayTeamScore = 1,
|
||||
Date = new DateTime(2025, 6, 12),
|
||||
TicketPrice = 20
|
||||
};
|
||||
|
||||
await context.AddRangeAsync(match1, match2, match3, match4);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
async Task ExplicitLoadingExample()
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user