↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Understanding IQueryable<T> in C#

The post presents IQueryable as a more advanced LINQ interface that works with sources such as SQL databases and in-memory collections. It highlights deferred execution, the Expression property as a tree built while the query is composed, and provider translation as the reason queries can be optimized for the underlying data store.

It also shows how extension methods can wrap filtering, sorting, pagination, and execution around IQueryable to keep query code cleaner. In the example, ApplyFilter, ApplySorting, ApplyPagination, and Execute are chained to build the final query before ToListAsync is called.

Fichamento
#

  • IQueryable is introduced as a LINQ interface meant for querying data from sources like SQL databases and in-memory collections.
  • Deferred execution means the query is not carried out until the data is actually needed, such as during enumeration in a foreach loop.
  • The Expression property holds a tree of query expressions that is assembled as the query is composed.
  • A data source provider can read that expression tree and translate it into something the source can use.
  • The post says this provider-based translation can improve efficiency and performance.
  • IQueryable supports common LINQ operations such as Where, OrderBy, Select, FirstOrDefault, LastOrDefault, and Single.
  • The article uses Entity Framework Core as an example of a provider that can translate the expression tree to SQL for stores like SQL Server or PostgreSQL.
  • Extension methods are used to centralize query logic for a blog repository.
  • ApplyFilter adds a title filter when the title value is not empty.
  • ApplySorting chooses an ordering based on PostSortOption, including comments, likes, views, or published date.
  • ApplyPagination applies Skip and Take based on page number and page size.
  • Execute calls ToListAsync with a cancellation token to materialize the query results.
  • The final code example combines these methods to make the query more fluent and less repetitive.