Home
C#
Queryable Use
This page was last reviewed on Nov 23, 2023.
Dot Net Perls
Queryable. What is the IQueryable interface used for? We find a Queryable class, an IQueryable interface, and an AsQueryable extension method.
Internal development. The IQueryable class is used to develop LINQ query providers. This feature is used to develop the .NET Framework itself.
A Queryable example. Here we have an int array and use AsQueryable (an extension method) on it. This gives us an IQueryable reference.
And We call Queryable.Average (a static method) with our IQueryable interface reference.
interface
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.
Info There is no advantage to using IQueryable over IEnumerable here. And it is simpler to use IEnumerable.
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.
A summary. 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.
dynamic
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 23, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.