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
.
Исключения
source
имеет значение null
.source
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 CreateQuery(Expression) методу класса, IQueryProvider представленного Provider свойством source
параметра.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. Ожидаемое поведение заключается в том, что он принимает первые count
элементы из начала source
.The expected behavior is that it takes the first count
elements from the start of source
.