Introduction:

 

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

Description:

In LINQ, ToList() method takes the elements from the given source and it returns a new List. So that means the input collection would be converted to type List.

In other words we can say, its convert input collection to a new list without reflecting any subsequent changes to the collection.

 

Example of LINQ ToList() Method

 

Following is the example of using LINQ ToList() to convert input collection to List in method.

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 = "Sekher", ID =  2 },
              new Employee() {  Name = "Sumit", ID =  3 },
            new Employee() {  Name = "Pankaj", ID = 4 },
            new Employee() {  Name = "Rahul", ID = 5 }};
 
           var result = Employee.ToList();
 
             foreach (var item in result)
            {
                Console.WriteLine("--------Employee----------");
                Console.WriteLine(" Name:"+item.Name);
                Console.WriteLine(" ID:" + item.ID);
            }
            Console.ReadLine();
        }
    }
}

 

As you see observe above example we are converting countries collection to List by using LINQ ToList () method.


Now we will run and see the output.


LINQ ToList Method

 

In this article we learned, how we can use LINQ ToList() Method to convert given collection items to new list.

 

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