Queryable.Take 方法

定义

重载

Take<TSource>(IQueryable<TSource>, Int32)

从序列的开头返回指定数量的相邻元素。

Take<TSource>(IQueryable<TSource>, Range)

返回序列中连续元素的指定范围。

Take<TSource>(IQueryable<TSource>, Int32)

从序列的开头返回指定数量的相邻元素。

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 的元素类型。

参数

source
IQueryable<TSource>

要从其返回元素的序列。

count
Int32

要返回的元素数量。

返回

IQueryable<TSource>

一个 IQueryable<T>,包含从 source 开始处的指定数量的元素。

例外

sourcenull

示例

下面的代码示例演示如何使用 Take<TSource>(IQueryable<TSource>, Int32) 它从序列的开头返回元素。

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)生成一个表示调用Take<TSource>(IQueryable<TSource>, Int32)本身为构造泛型方法的一个MethodCallExpression方法。 然后,MethodCallExpressionCreateQuery(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider

执行表示调用 Take<TSource>(IQueryable<TSource>, Int32) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是它从开头source获取第一个count元素。

适用于

Take<TSource>(IQueryable<TSource>, Range)

返回序列中连续元素的指定范围。

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

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

要从其返回元素的序列。

range
Range

要返回的元素范围,该元素的起始索引和结束索引从开头或结尾开始。

返回

IQueryable<TSource>

一个包含序列中source指定range元素的元素IQueryable<T>

例外

sourcenull

适用于