Introduction:

 

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

Description:

In LINQ, ToArray() method is used to convert the input elements in collection to an Array.

 

Example of LINQ ToList() Method

 

Following is the example of using LINQ ToArray () to convert input collection to an Array.

 

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.ToArray ();
 
             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 have a List of employees. By using ToArray () method we converted “employees” List to an array. To access these elements we have iterated the array in a foreach loop and displayed on the screen.

 

Now we will run and see the output.


 Use LINQ ToArray() Method


In this article we learned, how we can use LINQ ToArray to convert given collection items to array list using method syntax and query syntax based on our rquirements.

 

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