Skip to main content

Anti-Pattern: Client-Side Evaluation

Description

This anti-pattern occurs when a database query retrieves a large dataset into memory (RAM) before filtering it in the application layer. This usually happens when ToList() or ToArray() is called before a Where() clause on an IQueryable.

Impact

  • Performance: Retrieves unnecessary rows from the database, increasing network traffic and IO.
  • Memory: Can cause OutOfMemoryException if the table size grows.
  • Latency: The application must iterate over thousands of objects instead of letting the SQL engine optimize the filtering.

Detection

Look for usage of .ToList().Where(...) or .ToArray().Where(...) chains.

// ❌ BAD: Fetches ALL records, then filters in RAM
var activeUsers = db.Users
.ToList()
.Where(u => u.IsActive)
.ToList();

Refactoring

Move the filtering logic before materialization. Use Performance Stabilization Pillar 1: Query Pruning for complex builders.

// ✅ GOOD: Filters in SQL (WHERE IsActive = 1)
var activeUsers = db.Users
.Where(u => u.IsActive)
.ToList();

Exceptions

Client-side evaluation is sometimes necessary if the filter condition uses a C# method that cannot be translated to SQL (e.g., complex custom parsing). In such cases, comment explicitly why it is needed.

2026 Audit Findings

  • Occurrences: ~30 confirmed chains.
  • Severity: HIGH
  • Key Locations:
    • FrontAccountController.cs (Loading all Countries)
    • MaintenanceEmail tasks (Loading all WepOffices)
  • Case Study: OMGT Advanced Search - Postcode Loop