📎 Webclip
Understanding IQueryable<T> in C#
The post presents IQueryable
It also shows how extension methods can wrap filtering, sorting, pagination, and execution around IQueryable
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.