Querying Data with LINQ

Intermediate Updated May 20, 2025

๐Ÿ” Querying Data with LINQ

EF Core supports powerful querying using LINQ.

๐Ÿงช Basic Queries

var products = context.Products.ToList();

๐Ÿ”Ž Filtering and Ordering

var cheapProducts = context.Products
    .Where(p => p.Price < 50)
    .OrderBy(p => p.Name)
    .ToList();
var orders = context.Orders
    .Include(o => o.Customer)
    .ToList();

๐Ÿ“ Updating and Deleting Data

๐Ÿ”„ Updating Entities

var product = context.Products.Find(1);
product.Price = 99.99M;
context.SaveChanges();

โŒ Deleting Entities

var product = context.Products.Find(1);
context.Products.Remove(product);
context.SaveChanges();