Entity Framework Classic Query Deferred

Description

There are two types of IQueryable extension methods:

  • Deferred Methods: The query expression is modified but the query is not resolved (Select, Where, etc.).
  • Immediate Methods: The query expression is modified and the query is resolved (Count, First, etc.).

However, some features like Query Cache and Query Future cannot be used directly with the Immediate Method since the query is already resolved.

Query Deferred provides more flexibility to other features.

All LINQ IQueryable extension methods and overloads are supported:

Name Description Example
DeferredAll QueryDeferred extension method. Determines whether all elements of a sequence satisfy a condition. NET Core / NET Framework
DeferredAny QueryDeferred extension method. Determines whether a sequence contains any elements. NET Core / NET Framework
DeferredAverage QueryDeferred extension method. Computes the average of a sequence of Single values. NET Core / NET Framework
DeferredCount QueryDeferred extension method. Returns the number of elements in a sequence. NET Core / NET Framework
DeferredFirst QueryDeferred extension method. Returns the first element of a sequence. NET Core / NET Framework
DeferredFirstOrDefault QueryDeferred extension method. Returns the first element of a sequence, or a default value if the sequence contains no elements. NET Core / NET Framework
DeferredLongCount QueryDeferred extension method. Returns an Int64 that represents how many elements in a sequence satisfy a condition. NET Core / NET Framework
DeferredMax QueryDeferred extension method. Returns the maximum value in a sequence NET Core / NET Framework
DeferredMin QueryDeferred extension method. Returns the minimum value in a sequence NET Core / NET Framework
DeferredSingle QueryDeferred extension method. Returns the minimum value in a sequence of Single values. NET Core / NET Framework
DeferredSingleOrDefault QueryDeferred extension method. Returns the minimum value in a sequence of nullable Single values. NET Core / NET Framework
DeferredSum QueryDeferred extension method. Computes the sum of a sequence NET Core / NET Framework

Real-Life Scenarios

Query Cache

You want to cache the customer count (immediate method) with the Query Cache feature. You can defer the customer count with the DeferredCount method.

// ... Coming soon...

Coming soon

Query Future

You want to return the customer count (immediate method) with a paged list using the Query Future feature. You can defer the customer count with the DeferredCount method.

// Not do Select
var futurValue = context.Customers.DeferredCount().FutureValue();

context.Customers.Add(new Customer() { Name = "Customer_D", Description = "Description"});
context.SaveChanges();    

// SELECT COUNT(1) FROM Customers
Console.WriteLine("Count Customer is : " +   futurValue.Value);

Try it: NET Core | NET Framework

Documnentation

QueryDeferred

Methods
Name Description Example
Execute() Execute the deferred expression and return the result. NET Core / NET Framework
ExecuteAsync() Execute asynchrounously the deferred expression and return the result. NET Core / NET Framework
ExecuteAsync(CancellationToken cancellationToken) Execute asynchrounously the deferred expression and return the result. NET Core / NET Framework


Contents

Related