C# – First steps with C# and .NET – part 009 .

The IQueryable class is used to develop LINQ query providers.
The IQueryable interface inherits from IEnumerable.
IQueryable is used to implement LINQ providers for the .NET Framework internals.
We can use IQueryable in a similar way as IEnumerable, let’s see the next table all the differentiation:

Basis of differentiationIEnumerableIQueryable
NamespaceThe namespace in IEnumerable is System. CollectionsThe namespace in IQueryable is System. Linq
DerivationThere is no base interfaceDerives the base interface from IEnumerable
Deferred ExecutionSupports deferred executionDeferred execution is not possible
Lazy LoadingIEnumerable does not support lazy loadingIQuerytable supports lazy loading
FunctionalityIn the course of querying data from databases, it is possible to observe that IEnumerable executes by selecting query on the server-side, loading data in-memory on the client-side, and then filtering the data. As IEnumerable performs a lot more work than IQuerytable, it is much slower.In the process of querying data from databases, IQueryable can be seen executing a select query on the server-side with the help of its filters. In comparison to IEnumerable, it does less work and therefore showcases faster performance.
Suitable forLINQ to XML queries and LINQ to Object.LINQ to SQL query.
Custom QueriesCustom queries are not supported.Custom queries are supported with the help of CreateQuery as well as Execute methods.
Extension method
parameters
The extension methods compatible with IEnumerable are supportive of functional objects.The extension methods that are compatible with IQuerytable are known to take expression objects of the likes of an expression tree.
When used
  1. Working with read-only collections.
  2. Suppose the developer needs to read objects in the forward direction only without concern about thread safety.
  3. While querying data from Lists, Arrays, and other in-memory collections.
  1. While querying data from the out-memory collections of the likes of the remote database, service, etc.
  2. Useful for working with queryable data sources.
  3. When developers find it essential to apply filters on data right at the data source.
  4. When there is a need for applying paging composition. When the work revolves around external data sources.
  5. There is a requirement to load data in a deferred way.
  6. There is a need to utilize foreach for iterating a collection.

I use an example to show you how you can used it.
Open the Visual Studio and create a console project, I named this project console_IQueryable_test_001.
I use IQueryable in a similar way as IEnumerable to act upon collections of elements with a unified type.
AsQueryable is a method that allows the query to be converted to an instance of IQueryable.
I used AsQueryable on an IQueryable at the same time that you’d use AsEnumerable on an IEnumerable.
The source code is commented to understand how this working.

The output result of the running project is this:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.