Filter first, sort descending, then take two. Add a deterministic tie-breaker so repeated calls return stable results. Example: var top = await db.Products.AsNoTracking() .Where(p => p.IsActive) .OrderByDescending(p => p.Price) .ThenBy(p => p.Id) .Select(p => new { p.Id, p.Name, p.Price }) .Take(2) .ToListAsync(ct); For tenant-owned data, also filter with RestaurantId and OutletId from the authenticated context. An index beginning with those filters and including the ordering columns can help. Decide explicitly whether products tied at the second-highest price should all be returned; Take(2) returns exactly two rows.
Backbencher Developer · · 0 likes