C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: We call Queryable.Average (a static method) with our IQueryable interface reference.
InterfaceC# program that uses Queryable
using System;
using System.Linq;
class Program
{
static void Main()
{
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);
}
}
Output
11.6666666666667
Warning: There is no advantage to using IQueryable over IEnumerable here. And it is simpler to use IEnumerable.
IEnumerableC# program that uses IQueryable as argument type
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()}");
}
}
Output
Average: 0.5
Sum: 1
Average: 0.5
Sum: 1
So: IQueryable, and AsQueryable, are most useful for Framework developers, not users of the .NET Framework.
Quote: The IQueryable interface is intended for implementation by query providers. It is only supposed to be implemented by providers that also implement IQueryable<T>.
IQueryable Interface: Microsoft.com