Interface IQueryable<T>
Represents a query that can be executed against a data source. This interface is the base interface from which all LINQ provider implementations derive.
public interface IQueryable<out T> : IEnumerable<T>, IQueryable
Type Parameters
T
The type of the elements in the data source.
Remarks
The IQueryable<T> interface extends the IEnumerable<T> interface to enable the creation of LINQ queries that can be translated into data source-specific queries, such as SQL queries.
It provides methods for representing and executing queries against a data source. Providers that implement LINQ use this interface to represent queries that can be executed on their data source.
The IQueryable<T> interface is not intended to be implemented by application code.
Inheritance
IQueryable<T> inherits from IEnumerable<T> and IQueryable.
Methods
Provider
IQueryProvider Provider { get; }
Gets the element that associates with the specified data source.
Expression
Expression Expression { get; }
Gets the expression tree that is associated with the data source.
Extension Methods
The IQueryable<T> interface is augmented by a large number of extension methods, primarily found in the Enumerable class. These methods allow you to perform various operations on the queryable data source, such as filtering, projecting, sorting, and grouping.
Commonly Used Extension Methods:
Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate): Filters a sequence of values based on a predicate.Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector): Projects each element of a sequence into a new form.OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector): Sorts the elements of a sequence in ascending order.ToList<TSource>(this IEnumerable<TSource> source): Converts the elements of a sequence to aList<T>.ToArray<TSource>(this IEnumerable<TSource> source): Converts the elements of a sequence to an array.
Example
The following example demonstrates how to use IQueryable<T> with LINQ to query a list of products. In this case, the LINQ provider translates the query to be executed efficiently, even though the underlying data source is a simple list.
using System;
using System.Collections.Generic;
using System.Linq;
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
// Imagine this is a data source like a database table
var products = new List<Product>
{
new Product { Id = 1, Name = "Laptop", Price = 1200.50m },
new Product { Id = 2, Name = "Keyboard", Price = 75.00m },
new Product { Id = 3, Name = "Mouse", Price = 25.99m },
new Product { Id = 4, Name = "Monitor", Price = 300.00m },
new Product { Id = 5, Name = "Webcam", Price = 50.00m }
};
// This cast to IQueryable allows LINQ to build an expression tree
// that can be translated by a provider (e.g., Entity Framework)
IQueryable<Product> queryableProducts = products.AsQueryable();
// Query for products with a price greater than 100
var expensiveProducts = queryableProducts
.Where(p => p.Price > 100)
.OrderBy(p => p.Name)
.ToList(); // Executes the query
Console.WriteLine("Expensive Products:");
foreach (var product in expensiveProducts)
{
Console.WriteLine($"- {product.Name} (${product.Price:F2})");
}
}
}