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

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

Documnentation

QueryDeferred

Methods
Name Description Example
Execute() Execute the deferred expression and return the result. Try it
ExecuteAsync() Execute asynchrounously the deferred expression and return the result. Coming soon
ExecuteAsync(CancellationToken cancellationToken) Execute asynchrounously the deferred expression and return the result. Coming soon

In this Page