Note

Access to this page requires authorization. You can try signing in or .

Access to this page requires authorization. You can try .

Queryable.Take Method

Definition

Namespace:
System.Linq
Assemblies:
System.Core.dll, System.Linq.Queryable.dll
Assemblies:
netstandard.dll, System.Linq.Queryable.dll
Assembly:
System.Linq.Queryable.dll
Assembly:
System.Core.dll
Assembly:
netstandard.dll

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Overloads

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

Returns a specified number of contiguous elements from the start of a sequence.

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

Returns a specified range of contiguous elements from a sequence.

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

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

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);
[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<TSource> Take<TSource>(this System.Linq.IQueryable<TSource> source, int count);
static member Take : System.Linq.IQueryable<'Source> * int -> System.Linq.IQueryable<'Source>
[<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 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)

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IQueryable<TSource>

The sequence to return elements from.

count
Int32

The number of elements to return.

Returns

IQueryable<TSource>

An IQueryable<T> that contains the specified number of elements from the start of source.

Attributes

Exceptions

source is null.

Examples

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

Remarks

The Take<TSource>(IQueryable<TSource>, Int32) method generates a MethodCallExpression that represents calling Take<TSource>(IQueryable<TSource>, Int32) itself as a constructed generic method. It then passes the MethodCallExpression to the CreateQuery(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

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. The expected behavior is that it takes the first count elements from the start of source.

Applies to

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

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

Returns a specified range of contiguous elements from a sequence.

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);
[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<TSource> Take<TSource>(this System.Linq.IQueryable<TSource> source, Range range);
static member Take : System.Linq.IQueryable<'Source> * Range -> System.Linq.IQueryable<'Source>
[<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 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)

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IQueryable<TSource>

The sequence to return elements from.

range
Range

The range of elements to return, which has start and end indexes either from the start or the end.

Returns

IQueryable<TSource>

An IQueryable<T> that contains the specified range of elements from the source sequence.

Attributes

Exceptions

source is null.

Applies to


Feedback

Was this page helpful?