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

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 Framework | NET Core

Documnentation

QueryDeferred

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

Contents