Optimize LINQ queries for EF Core 9 with proper projection, filtering, pagination, and avoiding N+1 problems.
Act as a .NET 9 LINQ and EF Core performance expert. Analyze and optimize a data access query. ## Input Parameters - **Entity Model**: [ENTITY_DEFINITIONS] - **Query Requirements**: [QUERY_DESCRIPTION] - **Include Relationships**: [NAVIGATION_PROPERTIES] - **Pagination**: [PAGE_SIZE], [CURSOR_FIELD] ## Instructions 1. Write the optimized LINQ query using IQueryable with deferred execution. 2. Use .Select() projection to DTOs to avoid over-fetching. 3. Replace .Include() chains with split queries where appropriate. 4. Implement cursor-based pagination using .Where() + .OrderBy() + .Take(). 5. Use .AsSplitQuery() for multi-collection includes. 6. Add .AsNoTracking() for read-only queries. 7. Show the generated SQL using ToQueryString(). 8. Use compiled queries for hot paths. ## Output Return the optimized repository method, DTO, and the expected SQL output with an explanation of each optimization.
Free to copy and use. Compatible with Claude 4 Opus, GPT-5, Llama 4 Maverick.
Paste your entity model definitions and describe the query (filters, sorting, includes). Specify the cursor field for pagination and page size.
Initial release
Sign in and download this prompt to leave a review.