using Microsoft.EntityFrameworkCore; namespace Api; public static class OrderQueries { public const int PageSize = 20; // One page of a customer's orders with every navigation the order page needs. public static Task> PageAsync(AppDbContext db, int customerId, int page, CancellationToken ct) => db.Orders .Where(o => o.CustomerId == customerId) .OrderByDescending(o => o.OrderedAt) .ThenBy(o => o.Id) .Skip(page * PageSize) .Take(PageSize) .Include(o => o.Customer) .Include(o => o.Lines) .ThenInclude(l => l.Product) .Include(o => o.Payments) .AsSplitQuery() .AsNoTracking() .ToListAsync(ct); // Every order in a date range, the shape a report or an export loads. public static Task> RangeAsync(AppDbContext db, DateTime from, DateTime to, CancellationToken ct) => db.Orders .Where(o => o.OrderedAt >= from && o.OrderedAt < to) .Include(o => o.Customer) .Include(o => o.Lines) .ThenInclude(l => l.Product) .Include(o => o.Payments) .AsSplitQuery() .AsNoTracking() .ToListAsync(ct); // The same page with four references on the order instead of one: // two required (Customer, ShippingAddress) and two optional (BillingAddress, Carrier). public static Task> PageWithReferencesAsync(AppDbContext db, int customerId, int page, CancellationToken ct) => db.Orders .Where(o => o.CustomerId == customerId) .OrderByDescending(o => o.OrderedAt) .ThenBy(o => o.Id) .Skip(page * PageSize) .Take(PageSize) .Include(o => o.Customer) .Include(o => o.ShippingAddress) .Include(o => o.BillingAddress) .Include(o => o.Carrier) .Include(o => o.Lines) .ThenInclude(l => l.Product) .Include(o => o.Payments) .AsSplitQuery() .AsNoTracking() .ToListAsync(ct); // The same report with the four references. public static Task> RangeWithReferencesAsync(AppDbContext db, DateTime from, DateTime to, CancellationToken ct) => db.Orders .Where(o => o.OrderedAt >= from && o.OrderedAt < to) .Include(o => o.Customer) .Include(o => o.ShippingAddress) .Include(o => o.BillingAddress) .Include(o => o.Carrier) .Include(o => o.Lines) .ThenInclude(l => l.Product) .Include(o => o.Payments) .AsSplitQuery() .AsNoTracking() .ToListAsync(ct); }