Queryable.AsQueryable 方法

定義

多載

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

AsQueryable<TElement>(IEnumerable<TElement>)

將泛型 IEnumerable<T> 轉換成泛型 IQueryable<T>Converts a generic IEnumerable<T> to a generic IQueryable<T>.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts 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);
static member AsQueryable : System.Collections.IEnumerable -> System.Linq.IQueryable
<Extension()>
Public Function AsQueryable (source As IEnumerable) As IQueryable

參數

source
IEnumerable

所要轉換的序列。A sequence to convert.

傳回

代表輸入序列的 IQueryableAn IQueryable that represents the input sequence.

例外狀況

source 不會針對某些 T 實作 IEnumerable<T>source does not implement IEnumerable<T> for some T.

sourcenullsource is null.

備註

如果source AsQueryable(IEnumerable) implements IQueryable<T>的型別, 則會直接傳回它。If the type of source implements IQueryable<T>, AsQueryable(IEnumerable) returns it directly. 否則, 它會IQueryable<T>傳回, 它會在中Enumerable呼叫相等的查詢運算子方法, 而不是Queryable中的來執行查詢。Otherwise, it returns an IQueryable<T> that executes queries by calling the equivalent query operator methods in Enumerable instead of those in Queryable.

這個方法會source IEnumerable<T>假設為部分T執行。This method assumes that source implements IEnumerable<T> for some T. 在執行時間, 結果會是相同IQueryable<T> T的類型。At runtime, the result is of type IQueryable<T> for the same T. 當您不以靜態方式知道的類型T時, 這個方法適用于動態案例。This method is useful in dynamic scenarios when you do not statically know the type of T.

AsQueryable<TElement>(IEnumerable<TElement>)

將泛型 IEnumerable<T> 轉換成泛型 IQueryable<T>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);
static member AsQueryable : seq<'Element> -> System.Linq.IQueryable<'Element>
<Extension()>
Public Function AsQueryable(Of TElement) (source As IEnumerable(Of TElement)) As IQueryable(Of TElement)

類型參數

TElement

source 項目的類型。The type of the elements of source.

參數

source
IEnumerable<TElement>

所要轉換的序列。A sequence to convert.

傳回

IQueryable<TElement>

代表輸入序列的 IQueryable<T>An IQueryable<T> that represents the input sequence.

例外狀況

sourcenullsource is null.

範例

下列程式碼範例示範如何使用AsQueryable<TElement>(IEnumerable<TElement>)IEnumerable<T>轉換IQueryable<T>成。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

備註

如果source AsQueryable<TElement>(IEnumerable<TElement>) implements IQueryable<T>的型別, 則會直接傳回它。If the type of source implements IQueryable<T>, AsQueryable<TElement>(IEnumerable<TElement>) returns it directly. 否則, 它會IQueryable<T>傳回, 它會在中Enumerable呼叫相等的查詢運算子方法, 而不是Queryable中的來執行查詢。Otherwise, it returns an IQueryable<T> that executes queries by calling the equivalent query operator methods in Enumerable instead of those in Queryable.

適用於