What is the IQueryable
interface
used for? We find a Queryable class
, an IQueryable
interface
, and an AsQueryable
extension method.
The IQueryable
class
is used to develop LINQ query providers. This feature is used to develop the .NET Framework itself.
Here we have an int
array and use AsQueryable
(an extension method) on it. This gives us an IQueryable
reference.
Queryable.Average
(a static
method) with our IQueryable
interface
reference.using System; using System.Linq; var values = new int[] { 5, 10, 20 }; // We can convert an int array to IQueryable. // ... And then we can pass it to Queryable and methods like Average. double result = Queryable.Average(values.AsQueryable()); Console.WriteLine(result);11.6666666666667
IQueryable
The IQueryable
interface
inherits from IEnumerable
. We can use IQueryable
in a similar way as IEnumerable
to act upon collections of elements with a unified type.
IQueryable
over IEnumerable
here. And it is simpler to use IEnumerable
.using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { // List and array can be converted to IQueryable. List<int> list = new List<int>(); list.Add(0); list.Add(1); int[] array = new int[2]; array[0] = 0; array[1] = 1; // We can use IQueryable to treat collections with one type. Test(list.AsQueryable()); Test(array.AsQueryable()); } static void Test(IQueryable<int> items) { Console.WriteLine($"Average: {items.Average()}"); Console.WriteLine($"Sum: {items.Sum()}"); } }Average: 0.5 Sum: 1 Average: 0.5 Sum: 1
Development of an IQueryable
class
appears to be both complicated and difficult. It is probably best left to the implementors of the .NET Framework.
The .NET Framework contains features that are used for the development of the .NET Framework itself. Other features like "dynamic" are also focused on framework development.