linq-optimization-patterns
LINQ Optimization Patterns
Optimize LINQ queries and prevent performance anti-patterns in EF Core/ABP applications.
Summary
This skill covers efficient data access patterns for Entity Framework Core in ABP Framework applications. Focus areas: N+1 prevention, pagination, projections, and batch loading.
When to use: Reviewing queries, fixing slow endpoints, implementing list APIs.
Concepts
| Concept | Description | Details |
|---|---|---|
| Eager Loading | Load related entities in single query via JOIN | [patterns/eager-loading.md] |
| Projection | Transform to DTO at database level | [patterns/projection.md] |
| Batch Loading | Load related data for multiple parents in one query | [patterns/batch-loading.md] |
| Pagination | Efficient paging with count optimization | [patterns/pagination.md] |
Anti-Patterns
| Anti-Pattern | Severity | Detect | Impact | Details |
|---|---|---|---|---|
| N+1 Query | π΄ HIGH | foreach.*await.*Repo |
N+1 DB calls | [anti-patterns/n-plus-one.md] |
| Count After Pagination | π΄ HIGH | Count.*after.*ToList |
Double query | [anti-patterns/count-after-pagination.md] |
| Full Table Load | π΄ HIGH | GetListAsync() then filter |
Memory explosion | [anti-patterns/full-table-load.md] |
| In-Memory Join | π΄ HIGH | Multiple ToListAsync |
Cartesian in memory | [anti-patterns/in-memory-join.md] |
Decision Tree
Need related data for display?
ββ Single entity βββββββββββΊ Include() β [patterns/eager-loading.md]
ββ List of entities ββββββββΊ Batch load β [patterns/batch-loading.md]
ββ Only specific fields ββββΊ Projection β [patterns/projection.md]
Need paginated list with count?
ββ Always count FIRST ββββββΊ CountAsync() β [patterns/pagination.md]
Loading data then filtering?
ββ Filter in query βββββββββΊ WhereIf() β β
Correct
ββ Filter after ToList βββββΊ β Anti-pattern β [anti-patterns/full-table-load.md]
Quick Detection
Run these to find issues in your codebase:
# N+1: Query inside loop
grep -rn "foreach.*await.*Repository\|for.*await.*GetAsync" src/
# Count after pagination
grep -rn "\.Count().*ToList\|ToList.*\.Count()" src/
# Full table load
grep -rn "GetListAsync()" src/ | grep -v "Where\|Any\|First"
# In-memory filtering
grep -rn "ToListAsync().*\.Where\|GetListAsync.*\.Where" src/
Rules
| ID | Rule | Related |
|---|---|---|
| R001 | Execute CountAsync() before Skip/Take |
[anti-patterns/count-after-pagination.md] |
| R002 | Apply Where clauses before ToListAsync() |
[anti-patterns/full-table-load.md] |
| R003 | Load related entities via Include or batch, not in loops | [anti-patterns/n-plus-one.md] |
| R004 | Use Select() projection for DTO returns |
[patterns/projection.md] |
| R005 | Use AsNoTracking() for read-only queries |
[patterns/projection.md] |
| R006 | Use AsSplitQuery() for multiple collection includes |
[patterns/eager-loading.md] |
Checklist
Review checklist for LINQ queries:
- No
foreach/forwithawaitrepository calls inside -
CountAsync()executed beforeSkip/Take - No
GetListAsync()followed by.Where()in memory - No
FirstOrDefaultinsideSelectprojections - Related data loaded via
Includeor batch query -
Select()projection used for DTO returns -
AsNoTracking()used for read-only queries - Multiple collection includes use
AsSplitQuery()
Integration Points
This skill is used by:
- abp-code-reviewer: Query performance validation
- abp-developer: Efficient data access implementation
- debugger: Performance issue diagnosis
Related Skills
- efcore-patterns - Entity configuration, migrations
- abp-framework-patterns - Repository patterns
- dotnet-async-patterns - Async/await correctness
More from thapaliyabikendra/ai-artifacts
abp-infrastructure-patterns
ABP Framework cross-cutting patterns including authorization, background jobs, distributed events, multi-tenancy, and module configuration. Use when: (1) defining permissions, (2) creating background jobs, (3) publishing/handling distributed events, (4) configuring modules.
59clean-code-dotnet
Clean Code principles adapted for C#/.NET including naming, variables, functions, SOLID, error handling, and async patterns. Use when: (1) reviewing C# code, (2) refactoring for clarity, (3) writing new code, (4) code review feedback.
52abp-entity-patterns
ABP Framework domain layer patterns including entities, aggregates, repositories, domain services, and data seeding. Use when: (1) creating entities with proper base classes, (2) implementing custom repositories, (3) writing domain services, (4) seeding data.
51abp-api-implementation
Implement REST APIs in ABP Framework with AppServices, DTOs, pagination, filtering, and authorization. Use when building API endpoints for ABP applications.
46abp-service-patterns
ABP Framework application layer patterns including AppServices, DTOs, Mapperly mapping, Unit of Work, and common patterns like Filter DTOs and ResponseModel. Use when: (1) creating AppServices, (2) mapping DTOs with Mapperly, (3) implementing list filtering, (4) wrapping API responses.
44abp-contract-scaffolding
Generate ABP Application.Contracts layer scaffolding (interfaces, DTOs, permissions) from technical design. Enables parallel development by abp-developer and qa-engineer. Use when: (1) backend-architect needs to generate contracts, (2) preparing for parallel implementation workflow, (3) creating API contracts before implementation.
36