LINQ Take()
The LINQ Take operator is used to return the values from the given data structure. It takes an integer value as a parameter that represents the total number of elements to be retrieved from the given data structure.
Syntax:
Or
Where input_source is the data source.
Parameters:
It takes an integer value(n) which is used to return that number of elements from the given data structure.
Now consider a scenario where the value of n(integer) is greater than the total number of elements in the data structure. All the elements in the data structure are returned without any error.
Example 1:
Here, we will create a list that has 10 integers and get 5 elements using the Take operator using both the methods(Method and Query).
using System.Linq;
using System.Collections.Generic;
//create a class - Linuxhint
class Linuxhint
{
static public void Main(){
//create List named input
var input = new List() {34,56,78,12,34,53,56,78,90,100};
//return 5 integers
var result=input.Take(5);
Console.WriteLine("---Using Method---");
//display the result
foreach (int i in result)
{
Console.WriteLine(i);
}
Console.WriteLine("---Using Query---");
//display the result by returning 5 integers.
foreach (int j in (from element in input select element).Take(5))
{
Console.WriteLine(j);
}
}
}
Output:
Explanation:
1. So firstly, we created a list named input_numbers that holds 10 integer elements.
2. After that we are taking only 5 values using Take() with Method syntax.
3. Finally, we are displaying the result using a foreach loop.
Example 2:
Here, we will create a string array that has 4 strings and get 6 elements using the Take operator using both the methods(Method and Query).
using System.Linq;
//create a class - Linuxhint
class Linuxhint
{
static public void Main(){
//create string array named input
string[] input = {"Linuxhint","java","html","sravan"};
//return all strings
var result=input.Take(6);
Console.WriteLine("---Using Method---");
//display the result
foreach (string i in result)
{
Console.WriteLine(i);
}
Console.WriteLine("---Using Query---");
//display the result by returning all strings.
foreach (string j in (from element in input select element).Take(6))
{
Console.WriteLine(j);
}
}
}
Output:
You can see that all the elements from the string array were returned without any error.
Explanation:
Create a string array naemd input that holds 4 strings.
Use Method syntax to get all values using the Take() operator.
Use Query syntax to get all values using the Take() operator.
Conclusion
So we have seen how to return the elements using Take() operator in C# – LINQ. It takes an integer value as a parameter that represents the total number of elements to be retired from the given data structure. In each example, we implemented Take() with Method as well as Query syntax. Finally, we came to notice that if the value of an integer is greater than the total number of elements in the data structure, then all the elements in the data structure are returned without any error.