In LINQ, the Take operator is used to get the specified number of elements in sequence from the list/collection. The LINQ Take operator will return a specified number of elements from starting of the collection or list.
We will pass one parameter to the LINQ Take() operator to specify the number of elements to be returned.
Following is the syntax of using the LINQ Take operator to return the specified number of elements from the list/collection.
If you observe the above syntax we defined “Take(3)” this means it will return the first 3 elements from the list “countries”.
Following is the example of using the LINQ Take() operator in method syntax to return the specified number of elements from the list or collection.
In the above program, we have a string array that has countries. We want to display only the first three countries from the array in this example. So we have used the Take operator and passed the count to the operator to return the required number of elements from the array.
Following is the result of the LINQ Take() operator example to get a specified number of elements from the list using the LINQ Take operator.
If we use the LINQ Take() operator in query syntax, our example queries will be as shown below.
If we run the above code, we will get the same output as LINQ Take() operator in method syntax. Now we will execute the above example and see the result.
If we run the above program, we will get the result as below.
This is how we can use LINQ Take partition operator in query syntax and method syntax to get the specified number of elements from a sequence in list/collection.