C# Interview Question #122
What is the difference between Func<T, bool> and Expression<Func<T, bool>>?
Advanced LINQ, Expressions & Collections Senior Advanced
Quick Interview Answer
Func<T,bool> is a compiled delegate that can execute C# logic directly. Expression<Func<T,bool>> is a data structure representing the lambda expression.
Detailed Explanation
Func<T,bool> is a compiled delegate that can execute C# logic directly. Expression<Func<T,bool>> is a data structure representing the lambda expression.
When used with IEnumerable<T>, LINQ typically works with delegates and executes in .NET. When used with IQueryable<T>, query providers such as Entity Framework Core accept expression trees so they can translate the query into SQL.
This distinction matters when building reusable filters for database queries because not every C# operation in an expression tree can be translated by a database provider.
Code Example
Func<Product, bool> inMemory =
p => p.IsActive;
Expression<Func<Product, bool>> databaseFilter =
p => p.IsActive;
var query = context.Products.Where(databaseFilter);