When EF Core reads your LINQ statements, it attempts to translate everything into a SQL Server query (Server Evaluation). However, if you use a C# function inside your LINQ that SQL Server simply doesn't understand, the framework historically utilized Client Evaluation—downloading the data and calculating the result in C# RAM. This causes massive, hidden performance bottlenecks.
Imagine you wrote a custom C# method to calculate a user's geographical distance.
public bool IsNearby(int userId, int maximumMiles)
{
// Complex C# math code using local variables...
return true;
}
// ❌ The LINQ Query
var nearbyUsers = await _context.Users
// SQL Server DOES NOT have a function named 'IsNearby'.
// It has zero idea how to translate this C# method into SQL!
.Where(u => IsNearby(u.Id, 50))
.ToListAsync();
In older EF Core 2.1, the framework would quietly perform Client Evaluation. It would generate a SELECT * FROM Users query, download ALL 1 Million users into your Web API RAM, and run your C# IsNearby method manually on every single object. Developers never knew this was happening until their servers inevitably crashed.
Starting in EF Core 3.0, Microsoft aggressively patched this. EF Core now violently throws an InvalidOperationException during execution if it cannot translate the entire LINQ Where clause into SQL.
If you absolutely MUST use a specialized C# method to filter data, you must explicitly notify EF Core that you intend to filter on the Client Side. You accomplish this by forcing early execution using AsEnumerable() or ToListAsync().
var nearbyUsers = _context.Users
// 1. FILTER ON THE SERVER FIRST: Send the most restrictive possible query to SQL Server
.Where(u => u.State == "NY")
// 2. DOWNLOAD TO THE CLIENT: We safely download the remaining 500 New York users into RAM
.AsEnumerable()
// 3. FILTER ON THE CLIENT: Now we run the C# method safely on the small dataset!
.Where(u => IsNearby(u.Id, 50))
.ToList();
Q: "We are trying to filter users based on whether their `DateOfBirth.Year` is leap year. EF Core throws an InvalidOperationException saying it cannot translate `DateTime.IsLeapYear(u.DateOfBirth.Year)`. We do not want to use Client Evaluation because downloading 5 million users is impossible. How do we force Server Evaluation?"
Architect Answer: "If EF Core natively doesn't know how to translate a method, you must map it to a SQL Server User-Defined Function (UDF). You first create a physical UDF in SQL Server named `dbo.IsLeapYear(int year)`. Then, in your `DbContext`, you create an empty C# method tagged with `[DbFunction('IsLeapYear', 'dbo')]`. When you run your LINQ query using this mapped C# method, EF Core intercepts it and literally injects the SQL UDF name directly into the generated `WHERE` clause, ensuring 100% of the execution happens deep inside the SQL Server engine, zeroing out network memory bloat."