liveBook · Manning

IQueryable

As explained in Pro ASP.NET Core 7, Tenth Edition

IQueryable<T> is a .NET interface representing a queryable sequence whose query operations can be translated and executed by an underlying data provider.

IQueryable<T> represents a sequence that callers can query before retrieving its objects. It extends IEnumerable<T>. In an Entity Framework Core repository, exposing IQueryable<Product> lets callers use LINQ to request the products they need without knowing how the repository stores them. section 7.2.5

Listing 7.18 The contents of the IStoreRepository.cs file in the SportsStore/Models folder
namespace SportsStore.Models {
   public interface IStoreRepository {
       IQueryable<Product> Products { get; }
   }
}

The repository contract exposes Products as IQueryable<Product>, leaving query composition to its callers. section 7.2.5

Listing 7.19 The contents of the EFStoreRepository.cs file in the SportsStore/Models folder
namespace SportsStore.Models {
   public class EFStoreRepository : IStoreRepository {
       private StoreDbContext context;
               
       public EFStoreRepository(StoreDbContext ctx) {
           context = ctx;
       }
               
       public IQueryable<Product> Products => context.Products;
   }
}

The Entity Framework Core implementation supplies that property directly from StoreDbContext. section 7.2.5

Choose when the query should run:

  • Add LINQ operations to an IQueryable<T> before evaluating it to have the database retrieve the requested data, rather than retrieving everything and discarding unwanted objects. section 7.2.5 section 36.3.2
  • Do not repeatedly enumerate the same query by accident: each enumeration evaluates it again and sends another database query. section 7.2.5
  • When repeated reads need the same results, retain the retrieved list. Operations on those existing results do not require another database query. section 36.3.2

Sorting in a Blazor component

At /forms, applying OrderBy to an IQueryable<T> and then calling ToListAsync sends a new query; applying OrderBy to the existing result data sorts without one. Click the Sort buttons and check the ASP.NET Core server log to see the difference. section 36.3.2

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains query composition, database execution, and repeated enumeration through repository and component examples, without detailing provider translation internals.
Examples
Shows an IQueryable<Product> repository property and Sort buttons that contrast a new database query with sorting existing results.
What it assumes
Assumes familiarity with IEnumerable<T> and LINQ while defining IQueryable<T> through its database-repository use.
What you can do after
Apply it via Entity Framework Core to compose queries and recognize when enumeration or sorting sends another database request.
How it connects
Connects IQueryable<T> to IEnumerable<T>, LINQ, Entity Framework Core, the repository pattern, and Blazor component queries.

Also explained in

Compare how 4 books explain IQueryable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.