Introduction:

 

In this article we will learn, how to take method in LINQ or what is take method in LINQ C# Asp.net MVC with example.

Description:

In LINQ, The Take() method is used to get the specified number of elements in sequence from the list or collection. The Take () method returns the specified number of elements starting from the first element.

We will pass one parameter to LINQ Take() Method that will specify the number of elements to be returned.

 

Example of LINQ Take() Method

 

Following is the example of using LINQ Take() method in method to return specified number of elements from the list or collection.

 

using System;
using System.Collections.Generic;
using System.Linq;
namespace SQLTutorialsPoint
{
    class Employee
    {
        public string Name { get; set; }
        public int ID { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> Employee = new List<Employee>(){
            new Employee() {  Name = "Suresh",ID  = 1},
            new Employee() {  Name = "Rohini", ID=  2 },
              new Employee() {  Name = "Amit", ID=  3 },
            new Employee() {  Name = "Pankaj", ID = 4},
            new Employee() {  Name = "Suraj", ID = 5}};
 
            var result = Employee.Take(3);
 
            foreach (var item in result)
            {
                Console.WriteLine("Employee Name:"+item.Name+"  ID:" +item.ID);
            }
 
            Console.ReadLine();
        }
    }
}

 

 

 

 

As you see above example we have an employee List. Now we want to display only the first three employees from the List. So we have used the Take method and passed the number to the method so that it returns that number of elements from the array.

 

Now we will run and see the output.

 Use Take() Method in LINQ

 

In this article we learned, how we can use LINQ Take partition operator in query syntax and method  to get specified number of elements from sequence in list or collection.

 

I hope it will help to you after reading.

 

comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like