Returns a specified number of contiguous elements from the start of a sequence.
Assembly: System.Core (in System.Core.dll)
<[%$TOPIC/bb503062_en-us_VS_110_1_0_0_0_0%]> _
Public Shared Function Take(Of TSource) ( _
source As [%$TOPIC/bb503062_en-us_VS_110_1_0_0_0_1%](Of TSource), _
count As [%$TOPIC/bb503062_en-us_VS_110_1_0_0_0_2%] _
) As [%$TOPIC/bb503062_en-us_VS_110_1_0_0_0_3%](Of TSource)
public static [%$TOPIC/bb503062_en-us_VS_110_1_0_1_0_0%]<TSource> Take<TSource>(
this [%$TOPIC/bb503062_en-us_VS_110_1_0_1_0_1%]<TSource> source,
[%$TOPIC/bb503062_en-us_VS_110_1_0_1_0_2%] count
)
[[%$TOPIC/bb503062_en-us_VS_110_1_0_2_0_0%]]
public:
generic<typename TSource>
static [%$TOPIC/bb503062_en-us_VS_110_1_0_2_0_1%]<TSource>^ Take(
[%$TOPIC/bb503062_en-us_VS_110_1_0_2_0_2%]<TSource>^ source,
[%$TOPIC/bb503062_en-us_VS_110_1_0_2_0_3%] count
)
static member Take :
source:[%$TOPIC/bb503062_en-us_VS_110_1_0_3_0_0%]<'TSource> *
count:[%$TOPIC/bb503062_en-us_VS_110_1_0_3_0_1%] -> [%$TOPIC/bb503062_en-us_VS_110_1_0_3_0_2%]<'TSource>
Type Parameters
- TSource
The type of the elements of source.
Parameters
- source
- Type:
System.Collections.GenericIEnumerableTSource
The sequence to return elements from.
- count
- Type:
SystemInt32
The number of elements to return.
Return Value
Type: System.Collections.GenericIEnumerableTSourceAn IEnumerableT that contains the specified number of elements from the start of the input sequence.
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).| Exception | Condition |
|---|---|
| ArgumentNullException | source is . |
This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.
TakeTSource enumerates source and yields elements until count elements have been yielded or source contains no more elements.
If count is less than or equal to zero, source is not enumerated and an empty IEnumerableT is returned.
The TakeTSource and SkipTSource methods are functional complements. Given a sequence coll and an integer n, concatenating the results of coll.Take(n) and coll.Skip(n) yields the same sequence as coll.
In Visual Basic query expression syntax, a Take clause translates to an invocation of TakeTSource.
The following code example demonstrates how to use TakeTSource to return elements from the start of a sequence.
' Create an array of Integer values that represent grades.
Dim grades() As Integer = {59, 82, 70, 56, 92, 98, 85}
' Get the highest three grades by first sorting
' them in descending order and then taking the
' first three values.
Dim topThreeGrades As IEnumerable(Of Integer) = _
grades _
.OrderByDescending(Function(grade) grade) _
.Take(3)
' Display the results.
Dim output As New System.Text.StringBuilder("The top three grades are:" & vbCrLf)
For Each grade As Integer In topThreeGrades
output.AppendLine(grade)
Next
MsgBox(output.ToString())
' This code produces the following output:
'
' The top three grades are:
' 98
' 92
' 85
int[] grades = { 59, 82, 70, 56, 92, 98, 85 };
IEnumerable<int> topThreeGrades =
grades.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
*/
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.