Back to list
thapaliyabikendra

linq-optimization-patterns

by thapaliyabikendra

1🍴 0📅 Jan 24, 2026

SKILL.md


name: linq-optimization-patterns description: "LINQ and EF Core query optimization for ABP Framework. N+1 prevention, eager loading, projections, pagination." layer: 2 tech_stack: [dotnet, csharp, efcore, postgresql] topics: [query-optimization, n-plus-one, eager-loading, projections, performance] depends_on: [] complements: [efcore-patterns, abp-framework-patterns] keywords: [Include, ThenInclude, Select, AsNoTracking, AsSplitQuery, WhereIf, N+1, CountAsync]

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

ConceptDescriptionDetails
Eager LoadingLoad related entities in single query via JOIN[patterns/eager-loading.md]
ProjectionTransform to DTO at database level[patterns/projection.md]
Batch LoadingLoad related data for multiple parents in one query[patterns/batch-loading.md]
PaginationEfficient paging with count optimization[patterns/pagination.md]

Anti-Patterns

Anti-PatternSeverityDetectImpactDetails
N+1 Query🔴 HIGHforeach.*await.*RepoN+1 DB calls[anti-patterns/n-plus-one.md]
Count After Pagination🔴 HIGHCount.*after.*ToListDouble query[anti-patterns/count-after-pagination.md]
Full Table Load🔴 HIGHGetListAsync() then filterMemory explosion[anti-patterns/full-table-load.md]
In-Memory Join🔴 HIGHMultiple ToListAsyncCartesian 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

IDRuleRelated
R001Execute CountAsync() before Skip/Take[anti-patterns/count-after-pagination.md]
R002Apply Where clauses before ToListAsync()[anti-patterns/full-table-load.md]
R003Load related entities via Include or batch, not in loops[anti-patterns/n-plus-one.md]
R004Use Select() projection for DTO returns[patterns/projection.md]
R005Use AsNoTracking() for read-only queries[patterns/projection.md]
R006Use AsSplitQuery() for multiple collection includes[patterns/eager-loading.md]

Checklist

Review checklist for LINQ queries:

  • No foreach/for with await repository calls inside
  • CountAsync() executed before Skip/Take
  • No GetListAsync() followed by .Where() in memory
  • No FirstOrDefault inside Select projections
  • Related data loaded via Include or 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


Score

Total Score

60/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

Reviews

💬

Reviews coming soon