c sharp

C# LINQ ToList() Method

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

Language Integrated Query language (LINQ) is used to perform operations on the C# collections or normal data structures.

LINQ ToList() Method

The ToList() method in LINQ converts the given data source to the List like it converts Arrays, HashSet, etc. to List.

Syntax:

input_source.ToList();

Where input_source can be an Array, HashSet, etc.

Make sure you define the List like this:

List<int> converted_list = input_source.ToList();

We will understand this by exploring the following examples:

Example 1:

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

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

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
       //create array named input_numbers
      int[] input_numbers = {100,200,300,456};
     
       //create array named input_strings
      string[] input_strings = {"Linuxhint","Sravan"};
       
       //convert input_numbers array to list
      List<int> converted_list1 = input_numbers.ToList();  
     
       Console.WriteLine("Integer List: ");
           foreach (var values1 in converted_list1)
            {
                Console.WriteLine(values1);
            }
           
             //convert input_strings array to list
      List<string> converted_list2 = input_strings.ToList();  
     
       Console.WriteLine("String List: ");
           foreach (var values2 in converted_list2)
            {
                Console.WriteLine(values2);
            }
}
}

Output:

Explanation:

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

2. After that, we converted both arrays to List using the ToList() method through LINQ.

Example 2:

Here, we will create two HashSets with integers and strings. After that, we will convert them into a List, separately.

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","Sravan"};

       //convert input_numbers Hashset to list
      List<int> converted_list1 = input_numbers.ToList();  
     
       Console.WriteLine("Integer List: ");
           foreach (var values1 in converted_list1)
            {
                Console.WriteLine(values1);
            }
           
             //convert input_strings Hashset to list
      List<string> converted_list2 = input_strings.ToList();  
     
       Console.WriteLine("String List: ");
           foreach (var values2 in converted_list2)
            {
                Console.WriteLine(values2);
            }
}
}

Output:

Explanation:

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

2. After that, we converted both the HashSets to List using the ToList() method through LINQ and displayed the converted List using the foreach loop.

Conclusion

We learned the two data sources that are converted to List in C# through LINQ. We learned how to convert an array and HashSet to List using the ToList() method with two examples by considering the Integer 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