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 differentiation | IEnumerable | IQueryable |
---|---|---|
Namespace | The namespace in IEnumerable is System. Collections | The namespace in IQueryable is System. Linq |
Derivation | There is no base interface | Derives the base interface from IEnumerable |
Deferred Execution | Supports deferred execution | Deferred execution is not possible |
Lazy Loading | IEnumerable does not support lazy loading | IQuerytable supports lazy loading |
Functionality | In 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 for | LINQ to XML queries and LINQ to Object. | LINQ to SQL query. |
Custom Queries | Custom 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 |
|
|
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | using System; // add for using System.Collections.Generic; using System.Linq; namespace console_IQueryable_test_001 { class Program { static void Main(string[] args) { Console.WriteLine("Hello IQueryable 001 !"); // These list and array can be converted to IQueryable. // the list_for_IQueryable for testing list List<int> list_for_IQueryable = new List<int>(); list_for_IQueryable.Add(0); list_for_IQueryable.Add(1); list_for_IQueryable.Add(2); // the array_for_IQueryable for testing arrays int[] array_for_IQueryable = new int[3]; array_for_IQueryable[0] = 11; array_for_IQueryable[1] = 22; array_for_IQueryable[2] = 33; // We can use IQueryable to treat collections with type... // lists using conversion method AsQueryable Test_with_IQueryable(list_for_IQueryable.AsQueryable()); // arrays using conversion method AsQueryable Test_with_IQueryable(array_for_IQueryable.AsQueryable()); } // create method with IQueryable input static void Test_with_IQueryable(IQueryable<int> var_IQueryable) { Console.WriteLine($"First: {var_IQueryable.First()}"); Console.WriteLine($"Average: {var_IQueryable.Average()}"); Console.WriteLine($"Sum: {var_IQueryable.Sum()}"); Console.WriteLine($"Last: {var_IQueryable.Last()}"); Console.WriteLine($"Have elements : {var_IQueryable.Any()}"); } } } |
The output result of the running project is this:
1 2 3 4 5 6 7 8 9 10 11 | Hello IQueryable 001 ! First: 0 Average: 1 Sum: 3 Last: 2 Have elements : True First: 11 Average: 22 Sum: 66 Last: 33 Have elements : True |