Queryable.Take<TSource>(IQueryable<TSource>, Int32) 方法

定义

从序列的开头返回指定数量的相邻元素。Returns a specified number of contiguous elements from the start of a sequence.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ Take(System::Linq::IQueryable<TSource> ^ source, int count);
public static System.Linq.IQueryable<TSource> Take<TSource> (this System.Linq.IQueryable<TSource> source, int count);
static member Take : System.Linq.IQueryable<'Source> * int -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function Take(Of TSource) (source As IQueryable(Of TSource), count As Integer) As IQueryable(Of TSource)

类型参数

TSource

source 的元素类型。The type of the elements of source.

参数

source
IQueryable<TSource>

要从其返回元素的序列。The sequence to return elements from.

count
Int32

要返回的元素数量。The number of elements to return.

返回

IQueryable<TSource>

一个 IQueryable<T>,包含从 source 开始处的指定数量的元素。An IQueryable<T> that contains the specified number of elements from the start of source.

例外

sourcenullsource is null.

示例

下面的代码示例演示如何使用 Take<TSource>(IQueryable<TSource>, Int32) 从序列的开头返回元素。The following code example demonstrates how to use Take<TSource>(IQueryable<TSource>, Int32) to return elements from the start of a sequence.

int[] grades = { 59, 82, 70, 56, 92, 98, 85 };

// Sort the grades in descending order and take the first three.
IEnumerable<int> topThreeGrades =
    grades.AsQueryable().OrderByDescending(grade => grade).Take(3);

Console.WriteLine("The top three grades are:");
foreach (int grade in topThreeGrades)
    Console.WriteLine(grade);

/*
    This code produces the following output:

    The top three grades are:
    98
    92
    85
*/

Dim grades() As Integer = {59, 82, 70, 56, 92, 98, 85}

' Sort the grades in descending order and take the first three.
Dim topThreeGrades = _
    grades.AsQueryable().OrderByDescending(Function(grade) grade).Take(3)

Dim output As New System.Text.StringBuilder
output.AppendLine("The top three grades are:")
For Each grade As Integer In topThreeGrades
    output.AppendLine(grade)
Next

' Display the output.
MsgBox(output.ToString())

' This code produces the following output:

' The top three grades are:
' 98
' 92
' 85

注解

Take<TSource>(IQueryable<TSource>, Int32) 方法生成一个 MethodCallExpression,它表示作为构造的泛型方法调用 Take<TSource>(IQueryable<TSource>, Int32) 本身。The Take<TSource>(IQueryable<TSource>, Int32) method generates a MethodCallExpression that represents calling Take<TSource>(IQueryable<TSource>, Int32) itself as a constructed generic method. 然后,它将 MethodCallExpression 传递到 IQueryProviderCreateQuery(Expression) 方法(由 source 参数的 Provider 属性表示)。It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

因执行表示调用 Take<TSource>(IQueryable<TSource>, Int32) 的表达式树而发生的查询行为取决于 source 参数类型的实现。The query behavior that occurs as a result of executing an expression tree that represents calling Take<TSource>(IQueryable<TSource>, Int32) depends on the implementation of the type of the source parameter. 预期的行为是从 source开头获取第一个 count 元素。The expected behavior is that it takes the first count elements from the start of source.

适用于