LINQ Interview Question #102

What is the difference between Func<T,bool> and Expression<Func<T,bool>>?

LINQ Performance, EF Core LINQ, Expressions & Advanced Queries Senior Advanced

Quick Interview Answer

Func<T,bool> is an executable delegate. It contains compiled behavior that .NET can invoke directly. Expression<Func<T,bool>> represents the same logical operation as an inspectable expression tree.

Detailed Explanation

Func<T,bool> is an executable delegate. It contains compiled behavior that .NET can invoke directly. Expression<Func<T,bool>> represents the same logical operation as an inspectable expression tree.

LINQ to Objects commonly uses delegates, while IQueryable providers commonly require expression trees so they can analyze and translate the query.

Code Example

Func<Product, bool> inMemoryFilter =
    p => p.Price > 100;

Expression<Func<Product, bool>> dbFilter =
    p => p.Price > 100;