If you've ever used Language Integrated Query (LINQ) before, you'll feel right at home with EF Core. The DbSet
collection, which we've used to manipulate objects' states, implements the IQueryable
interface that defines many querying operators that are later transformed into SQL statements by the database provider you've used.
For example, the GiveNTake
application allows the user to search for products that were published on a specific date. Here is a shortened version of how it is done:
[HttpGet("search/{date:datetime}/{keyword}/")] public async Task<IActionResult> Search(DateTime date, string keyword) { var products = await _context.Products .Where(p => p.Title.Contains(keyword)) .Where(p => p.PublishDate.Date == date.Date) .ToListAsync(); // returning a response with the found products }
The Where
operator creates a filter on the IQueryable<Product>
that the Products
DbSet
collection implements. LINQ has a composable...