Edit

Share via


Queryable.AsQueryable Method

Definition

Overloads

AsQueryable(IEnumerable)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

Converts an IEnumerable to an IQueryable.

public:
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable ^ AsQueryable(System::Collections::IEnumerable ^ source);
public static System.Linq.IQueryable AsQueryable(this System.Collections.IEnumerable source);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating in-memory collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Enumerating in-memory collections as IQueryable can require unreferenced code because expressions referencing IQueryable extension methods can get rebound to IEnumerable extension methods. The IEnumerable extension methods could be trimmed causing the application to fail at runtime.")]
public static System.Linq.IQueryable AsQueryable(this System.Collections.IEnumerable source);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Enumerating in-memory collections as IQueryable can require unreferenced code because expressions referencing IQueryable extension methods can get rebound to IEnumerable extension methods. The IEnumerable extension methods could be trimmed causing the application to fail at runtime.")]
public static System.Linq.IQueryable AsQueryable(this System.Collections.IEnumerable source);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Enumerating in-memory collections as IQueryable can require unreferenced code because expressions referencing IQueryable extension methods can get rebound to IEnumerable extension methods. The IEnumerable extension methods could be trimmed causing the application to fail at runtime.")]
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static System.Linq.IQueryable AsQueryable(this System.Collections.IEnumerable source);
static member AsQueryable : System.Collections.IEnumerable -> System.Linq.IQueryable
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating in-memory collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Enumerating in-memory collections as IQueryable can require unreferenced code because expressions referencing IQueryable extension methods can get rebound to IEnumerable extension methods. The IEnumerable extension methods could be trimmed causing the application to fail at runtime.")>]
static member AsQueryable : System.Collections.IEnumerable -> System.Linq.IQueryable
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Enumerating in-memory collections as IQueryable can require unreferenced code because expressions referencing IQueryable extension methods can get rebound to IEnumerable extension methods. The IEnumerable extension methods could be trimmed causing the application to fail at runtime.")>]
static member AsQueryable : System.Collections.IEnumerable -> System.Linq.IQueryable
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Enumerating in-memory collections as IQueryable can require unreferenced code because expressions referencing IQueryable extension methods can get rebound to IEnumerable extension methods. The IEnumerable extension methods could be trimmed causing the application to fail at runtime.")>]
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member AsQueryable : System.Collections.IEnumerable -> System.Linq.IQueryable
<Extension()>
Public Function AsQueryable (source As IEnumerable) As IQueryable

Parameters

source
IEnumerable

A sequence to convert.

Returns

An IQueryable that represents the input sequence.

Attributes

Exceptions

source does not implement IEnumerable<T> for some T.

source is null.

Remarks

If the type of source implements IQueryable<T>, AsQueryable(IEnumerable) 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.

This method assumes that source implements IEnumerable<T> for some T. At runtime, the result is of type IQueryable<T> for the same T. This method is useful in dynamic scenarios when you do not statically know the type of T.

Applies to

AsQueryable<TElement>(IEnumerable<TElement>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

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

public:
generic <typename TElement>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TElement> ^ AsQueryable(System::Collections::Generic::IEnumerable<TElement> ^ source);
public static System.Linq.IQueryable<TElement> AsQueryable<TElement>(this System.Collections.Generic.IEnumerable<TElement> source);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating in-memory collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Enumerating in-memory collections as IQueryable can require unreferenced code because expressions referencing IQueryable extension methods can get rebound to IEnumerable extension methods. The IEnumerable extension methods could be trimmed causing the application to fail at runtime.")]
public static System.Linq.IQueryable<TElement> AsQueryable<TElement>(this System.Collections.Generic.IEnumerable<TElement> source);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Enumerating in-memory collections as IQueryable can require unreferenced code because expressions referencing IQueryable extension methods can get rebound to IEnumerable extension methods. The IEnumerable extension methods could be trimmed causing the application to fail at runtime.")]
public static System.Linq.IQueryable<TElement> AsQueryable<TElement>(this System.Collections.Generic.IEnumerable<TElement> source);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Enumerating in-memory collections as IQueryable can require unreferenced code because expressions referencing IQueryable extension methods can get rebound to IEnumerable extension methods. The IEnumerable extension methods could be trimmed causing the application to fail at runtime.")]
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static System.Linq.IQueryable<TElement> AsQueryable<TElement>(this System.Collections.Generic.IEnumerable<TElement> source);
static member AsQueryable : seq<'Element> -> System.Linq.IQueryable<'Element>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating in-memory collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Enumerating in-memory collections as IQueryable can require unreferenced code because expressions referencing IQueryable extension methods can get rebound to IEnumerable extension methods. The IEnumerable extension methods could be trimmed causing the application to fail at runtime.")>]
static member AsQueryable : seq<'Element> -> System.Linq.IQueryable<'Element>
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Enumerating in-memory collections as IQueryable can require unreferenced code because expressions referencing IQueryable extension methods can get rebound to IEnumerable extension methods. The IEnumerable extension methods could be trimmed causing the application to fail at runtime.")>]
static member AsQueryable : seq<'Element> -> System.Linq.IQueryable<'Element>
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Enumerating in-memory collections as IQueryable can require unreferenced code because expressions referencing IQueryable extension methods can get rebound to IEnumerable extension methods. The IEnumerable extension methods could be trimmed causing the application to fail at runtime.")>]
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member AsQueryable : seq<'Element> -> System.Linq.IQueryable<'Element>
<Extension()>
Public Function AsQueryable(Of TElement) (source As IEnumerable(Of TElement)) As IQueryable(Of TElement)

Type Parameters

TElement

The type of the elements of source.

Parameters

source
IEnumerable<TElement>

A sequence to convert.

Returns

IQueryable<TElement>

An IQueryable<T> that represents the input sequence.

Attributes

Exceptions

source is null.

Examples

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
*/
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

MsgBox("The NodeType of the expression tree is: " _
    & expressionTree.NodeType.ToString())
MsgBox("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

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.

Applies to