Queryable.AsQueryable<TElement> Method (IEnumerable<TElement>)

Converts a generic IEnumerable<T> to a generic IQueryable<T>.

Namespace:  System.Linq
Assembly:  System.Core (in System.Core.dll)

public static IQueryable<TElement> AsQueryable<TElement>(
	this IEnumerable<TElement> source
)

Type Parameters

TElement

The type of the elements of source.

Parameters

source
Type: System.Collections.Generic.IEnumerable<TElement>

A sequence to convert.

Return Value

Type: System.Linq.IQueryable<TElement>
An IQueryable<T> that represents the input sequence.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable<TElement>. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

ExceptionCondition
ArgumentNullException

source is null.

If the type of source implements IQueryable<T>, AsQueryable<TElement>(IEnumerable<TElement>) returns it directly. Otherwise, it returns an IQueryable<T> that executes queries by calling the equivalent query operator methods in Enumerable instead of those in Queryable.

The following code example demonstrates how to use AsQueryable<TElement>(IEnumerable<TElement>) to convert an IEnumerable<T> to an IQueryable<T>.

List<int> grades = new List<int> { 78, 92, 100, 37, 81 };

// Convert the List to an IQueryable<int>.
IQueryable<int> iqueryable = grades.AsQueryable();

// Get the Expression property of the IQueryable object.
System.Linq.Expressions.Expression expressionTree =
    iqueryable.Expression;

Console.WriteLine("The NodeType of the expression tree is: "
    + expressionTree.NodeType.ToString());
Console.WriteLine("The Type of the expression tree is: "
    + expressionTree.Type.Name);

/*
    This code produces the following output:

    The NodeType of the expression tree is: Constant
    The Type of the expression tree is: EnumerableQuery`1
*/

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft