liveBook · Manning

IQueryable

As explained in Building Web APIs with ASP.NET Core

IQueryable<T> is a generic .NET interface representing a queryable data source of a specified element type whose query can be composed and translated by a provider.

IQueryable<T> represents a query over elements of type T. With Entity Framework Core (EF Core), it lets you assemble a database query in memory before executing it. For example, ApplicationDbContext.BoardGames is a DbSet<BoardGame>, and DbSet<T> implements IQueryable<T>. You can therefore use LINQ operators on BoardGames without writing SQL yourself. section 5.1.3 section 5.0

A board-game API read operation follows three steps:

  • Start with _context.BoardGames to represent the board-game records. section 5.1.3
  • Compose the query with LINQ operators. Where(b => b.Year == 2020) restricts the results; OrderBy(b => b.Name) sorts them. You can chain the operators or add them on separate lines. section 5.1.3
  • Execute the query when you need its results. ToArray() makes EF Core fetch the records and return an array of BoardGame entities. section 5.1.3 section 5.2

The composed representation is an expression tree. EF Core translates it into a database query when the IQueryable<T> is executed, rather than when each operator is added. Against SQL Server, filtering by year and ordering by name corresponds to a SELECT with WHERE and ORDER BY clauses. In a controller, constructor injection makes ApplicationDbContext available to action methods, allowing a real database query to replace manually generated response data. section 5.1.3 section 5.2

var query = _context.BoardGames
   .Where(b => b.Year == 2020)
   .OrderBy(b => b.Name);

This listing shows a query built by chaining a year filter and a name sort; it does not execute the query. section 5.1.3

IQueryable<T> can also be returned from a GraphQL query method. A method returning context.BoardGames gives the HotChocolate runtime a queryable source; its paging, projection, filtering, and sorting attributes let the runtime shape database queries. This keeps the DbSet as the starting point while the GraphQL request determines which supported operations are applied. section 10.2.3

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains DbSet<T> implementation, LINQ composition, expression-tree translation, and execution with ToArray(), without detailing translation internals.
Examples
Demonstrates filtering board games by year, sorting by name, fetching a controller response, and returning DbSets from GraphQL query methods.
What it assumes
Assumes basic database manipulation operations and C# polymorphism; introduces the LINQ query-building role without requiring specific SQL knowledge.
What you can do after
Apply it via EF Core to compose and execute controller queries or provide queryable sources for HotChocolate GraphQL methods.
How it connects
Connects IQueryable<T> to LINQ, DbSet<T>, expression trees, dependency injection, SQL queries, and GraphQL.

Also explained in

Compare how 4 books explain IQueryable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.