Add example basic query functions.
This commit is contained in:
parent
61b0e99f94
commit
2ef3859d2a
@ -14,19 +14,50 @@ var teamFour = await context.Teams.SingleOrDefaultAsync(t => t.TeamId == 3);
|
||||
// Rather than raise: "System.InvalidOperationException: Sequence contains no elements."
|
||||
var coach = await context.Coaches.FirstOrDefaultAsync();
|
||||
|
||||
// Selection based on finding the Primary Key.
|
||||
var teamBasedOnId = await context.Teams.FindAsync(5);
|
||||
if (teamBasedOnId != null)
|
||||
await GetAllTeamsQuerySyntax("Neo");
|
||||
|
||||
async Task GetAllTeamsQuerySyntax(string searchTerm)
|
||||
{
|
||||
Console.WriteLine(teamBasedOnId.Name);
|
||||
var teams = await (
|
||||
from team in context.Teams
|
||||
where EF.Functions.Like(team.Name, $"%{searchTerm}%")
|
||||
select team)
|
||||
.ToListAsync();
|
||||
foreach (var team in teams)
|
||||
{
|
||||
Console.WriteLine(team.Name);
|
||||
}
|
||||
}
|
||||
|
||||
void PrintTeams()
|
||||
async Task PrintTeams()
|
||||
{
|
||||
var teams = context.Teams.ToList();
|
||||
var teams = await context.Teams.ToListAsync();
|
||||
|
||||
foreach (var team in teams)
|
||||
{
|
||||
Console.WriteLine(team.Name);
|
||||
}
|
||||
}
|
||||
|
||||
async Task PrintFilteredTeams()
|
||||
{
|
||||
Console.WriteLine("Enter search term: ");
|
||||
var searchTerm = Console.ReadLine();
|
||||
|
||||
// var filteredTeams = await context.Teams.Where(t => t.Name.Contains(searchTerm)).ToListAsync();
|
||||
var filteredTeams = await context.Teams.Where(t => EF.Functions.Like(t.Name, $"%{searchTerm}%")).ToListAsync();
|
||||
foreach (var team in filteredTeams)
|
||||
{
|
||||
Console.WriteLine(team.Name);
|
||||
}
|
||||
}
|
||||
|
||||
async Task PrintTeamById(int id)
|
||||
{
|
||||
// Selection based on finding the Primary Key.
|
||||
var teamBasedOnId = await context.Teams.FindAsync(id);
|
||||
if (teamBasedOnId != null)
|
||||
{
|
||||
Console.WriteLine(teamBasedOnId.Name);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user