Поделиться через


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

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
<ExtensionAttribute> _
Public Shared Function AsQueryable(Of TElement) ( _
    source As IEnumerable(Of TElement) _
) As IQueryable(Of TElement)
public static IQueryable<TElement> AsQueryable<TElement>(
    this IEnumerable<TElement> source
)

Type Parameters

  • TElement
    The type of the elements of source.

Parameters

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.

Exceptions

Exception Condition
ArgumentNullException

source is nulla null reference (Nothing in Visual Basic).

Remarks

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.

Examples

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

      Dim grades As New List(Of Integer)(New Integer() {78, 92, 100, 37, 81})

      ' Convert the List to an IQueryable<int>.
      Dim iqueryable As IQueryable(Of Integer) = grades.AsQueryable()

      ' Get the Expression property of the IQueryable object.
      Dim expressionTree As System.Linq.Expressions.Expression = _
          iqueryable.Expression

      outputBlock.Text &= "The NodeType of the expression tree is: " _
          & expressionTree.NodeType.ToString() & vbCrLf
      outputBlock.Text &= "The Type of the expression tree is: " _
          & expressionTree.Type.Name & vbCrLf

      ' This code produces the following output:
      '
      ' The NodeType of the expression tree is: Constant
      ' The Type of the expression tree is: EnumerableQuery`1

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;

outputBlock.Text += "The NodeType of the expression tree is: "
    + expressionTree.NodeType.ToString() + "\n";
outputBlock.Text += "The Type of the expression tree is: "
    + expressionTree.Type.Name + "\n";

/*
    This code produces the following output:

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

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.