liveBook · Manning

IQueryable

As explained in ASP.NET Core in Action, 3e

IQueryable is a .NET interface for representing queries whose expression trees can be translated and executed by a data source.

IQueryable represents a query that a data source can translate and execute. In EF Core, a DbSet<Recipe> exposes IQueryable, allowing LINQ filters and projections to be composed before an execution method sends the query to the database. section 12.4.2

An EF Core query commonly follows this sequence:

  • Start with a DbSet property on the application data context. section 12.4.2
  • Add LINQ Where clauses to filter records and Select clauses to project only the needed fields. section 12.4.2
  • Call ToListAsync(), ToArrayAsync(), SingleAsync(), or a corresponding synchronous method to generate and execute the database query. section 12.4.2
  • Expect unsupported expressions to run in C# after EF Core retrieves the required database fields. section 12.4.2

Filtering and projecting recipes

A query can exclude soft-deleted recipes and create view models directly: _context.Recipes.Where(r => !r.IsDeleted).Select(r => new RecipeSummaryViewModel { Id = r.RecipeId, Name = r.Name }).ToListAsync(). EF Core selects the columns needed for the projection and builds the returned view models. section 12.4.2

Listing 12.7 Loading a list of items using EF Core in RecipeService
public async Task<ICollection<RecipeSummaryViewModel>> GetRecipes()
{
    return await _context.Recipes                              #1
        .Where(r => !r.IsDeleted)
        .Select(r => new RecipeSummaryViewModel                #2
        {                                                      #2
            Id = r.RecipeId,                                   #2
            Name = r.Name,                                     #2
            TimeToCook = $"{r.TimeToCook.TotalMinutes}mins"    #2
        })
        .ToListAsync();                                        #3
}
#1 A query starts from a DbSet property.
#2 EF Core queries only the Recipe columns it needs to map the view model correctly.
#3 Executes the SQL query and creates the final view models

Shows the complete EF Core pattern from DbSet, through filtering and view-model projection, to asynchronous execution. section 12.4.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains composition, SQL translation, execution methods, projection, and limited client-side evaluation without covering provider internals.
Examples
Uses a RecipeService query that filters deleted recipes, projects RecipeSummaryViewModel objects, and calls ToListAsync().
What it assumes
Assumes familiarity with LINQ and EF Core DbContext and DbSet properties.
What you can do after
Apply it via EF Core to filter, project, and asynchronously load database records efficiently.
How it connects
Connects LINQ, Entity Framework Core, DbSet, SQL, DbContext, and view models through query composition and projection.

Also explained in

Compare how 4 books explain IQueryable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.