c sharp

C# LINQ ToArray() Method

We will see how to convert the given data to the Array in C# using the LINQ ToArray() method.

Language Integrated Query language (LINQ) is used to perform operations on the C# collections or normal data structures. We can also perform the database query operations with LINQ. LINQ supports many methods and functions that are used for data manipulation like Updation, deletion and Insertion, etc.

LINQ ToArray()

The ToArray() method in LINQ converts the given data source to an Array like it converts List, HashSet, Sorted set, etc. to an Array.

Syntax:

(from i in input_source select i).ToArray();

Where the input_source can be a List, HashSet, etc.

We will further understand this by the following examples:

Example 1:

Here, we will create two Lists with integers and strings. After that, we will convert them into Arrays.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
       //create List named input_numbers
       var input_numbers = new  List<int>() {100,200,300,456};
     
  //create List named input_strings
       var input_strings = new  List<string>() {"Linuxhint","Java"};
       
       //convert input_numbers to array
      var converted_array1 = (from i in input_numbers select i).ToArray();  
     
       Console.WriteLine("Array: ");
           foreach (var values1 in converted_array1)
            {
                Console.WriteLine(values1);
            }
     

       //convert input_strings List to Array
      var converted_array2 = (from i in input_strings select i).ToArray();  
     
       Console.WriteLine("Array: ");
           foreach (var values2 in converted_array2)
            {
                Console.WriteLine(values2);
            }
    }
           
}

Output:

Explanation:

1. First, we created two Lists with Integer and String type. The Input_numbers refers to the Integer List and the Input_strings refers to the String list.

2. After that, we converted both the Lists to an Array using the ToArray() method through LINQ.

Example 2:

Here, we will create two HashSets with integers and strings. After that, we will convert them into an Array.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
       //create HashSet named input_numbers
       var input_numbers = new  HashSet<int>() {100,200,300,456};
     
  //create HashSet named input_strings
       var input_strings = new  HashSet<string>() {"Linuxhint","Java"};
       
       //convert input_numbers HashSet to array
      var converted_array1 = (from i in input_numbers select i).ToArray();  
     
       Console.WriteLine("Array: ");
           foreach (var values1 in converted_array1)
            {
                Console.WriteLine(values1);
            }
     

       //convert input_strings HashSet to Array
      var converted_array2 = (from i in input_strings select i).ToArray();  
     
       Console.WriteLine("Array: ");
           foreach (var values2 in converted_array2)
            {
                Console.WriteLine(values2);
            }
    }
           
}

Output:

Explanation:

1. First, we created two HashSets with Integer and String type. The Input_numbers refers to the Integer HashSet and the Input_strings refers to the String HashSet.

2. After that, we converted both the HashSets to an Array using the ToArray() method through LINQ.

Example 3:

Here, we will create two SortedSets with integers and strings. After that, we will convert them into an Array.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
       //create SortedSet named input_numbers
       var input_numbers = new  SortedSet<int>() {100,200,300,456};
     
  //create SortedSet named input_strings
       var input_strings = new  SortedSet<string>() {"Linuxhint","Java"};
       
       //convert input_numbers SortedSet to array
      var converted_array1 = (from i in input_numbers select i).ToArray();  
     
       Console.WriteLine("Array: ");
           foreach (var values1 in converted_array1)
            {
                Console.WriteLine(values1);
            }
     

       //convert input_strings SortedSet to Array
      var converted_array2 = (from i in input_strings select i).ToArray();  
     
       Console.WriteLine("Array: ");
           foreach (var values2 in converted_array2)
            {
                Console.WriteLine(values2);
            }
    }
           
}

Output:

Explanation:

1. First, we created two SortedSets with Integer and String type. The Input_numbers refers to the Integer SortedSet and the Input_strings refers to the String SortedSet.

2. After that, we converted both the SortedSets to an Array using the ToArray() method through LINQ.

Conclusion

We learned the two data sources that are converted to Array in C# through LINQ. We learned how to convert the List, SortedSet, and HashSet to List using the ToArray() method with three examples by considering the Inter and String type values.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain