Basic LINQ queries
This book is not intended to be a LINQ handbook. However, we will show you some basic LINQ queries in this section:
- Querying the data
- Filtering the data
- Sorting the data
- Paging the data
- Creating the data
- Updating the data
- Deleting the data
Querying the data
The DbSet<Invoice> Invoices
property in the InvoiceDbContext
class represents a collection of the Invoice
entity. We can use LINQ methods to query the data. For example, we can use the ToListAsync()
method to retrieve all the invoices from the database:
var invoices = await _context.Invoices.ToListAsync();
That is how the GetInvoices
action method works.
To find a specific invoice, we can use the FindAsync()
method, as shown in the GetInvoice()
action method:
var invoice = await _context.Invoices.FindAsync(id);
The FindAsync()
method accepts the primary key value as the parameter. EF Core will translate the FindAsync()
method to the SQL SELECT
statement...