Author : Rahul
Last Modified : 13-Jul-2021
Complexity : Beginner

Conversion Operator in LINQ


Conversion Operator in LINQ is used to convert the type of the elements into a list or collection. There are different conversion Operators are available in LINQ as follow:

  1. TOARRAY
  2. TOLIST
  3. TODICTIONARY
  4. OFTYPE

Now we will discuss each operator one by one.

TOARRAY

TOARRAY operator is used to convert the collection into an array. ToArray keyword is used for this purpose. Below is the example in this category.

Query to convert a List of numbers into an array.

class Program
 {
   static void Main(string[] args)
   {
     List<int> numbers = new List<int>(){ 3, 6, 9, 12, 15 };
     
     //Linq Query
     var result_Linq = (from p in numbers select p).ToArray();
     
     //Lambda Expression
     var result_Lambda = numbers.Select(p=>p).ToArray();
     
     //Display Linq Query Result
     Console.WriteLine("Numbers (Using Linq):");
     foreach (int item in result_Linq)
     {
       Console.WriteLine(item);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Numbers (Using Lambda):");
     foreach (int item in result_Lambda)
     {
       Console.WriteLine(item);
     }
     
     Console.Read();
   }
 }

Output

Numbers (Using Linq):
3
6
9
12
15
Numbers (Using Lambda):
3
6
9
12
15


TOLIST

TOLIST operator is used to convert the collection into a list. ToList keyword is used for this purpose. Below is the example in this category.

Query to convert a number array into a list of numbers.

class Program
 {
   static void Main(string[] args)
   {
     int[] numbers = { 3, 6, 9, 12, 15 };
     
     //Linq Query
     var result_Linq = (from p in numbers select p).ToList();
     
     //Lambda Expression
     var result_Lambda = numbers.Select(p=>p).ToList();
     
     //Display Linq Query Result
     Console.WriteLine("Numbers (Using Linq):");
     foreach (int item in result_Linq)
     {
       Console.WriteLine(item);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Numbers (Using Lambda):");
     foreach (int item in result_Lambda)
     {
       Console.WriteLine(item);
     }
     
     Console.Read();
   }
 }

Output

Numbers (Using Linq):
3
6
9
12
15
Numbers (Using Lambda):
3
6
9
12
15


TODICTIONARY

TODICTIONARY operator is used to convert the collection into a dictionary based on some key. ToDictionary keyword is used for this purpose. Below is the example in this category.

Query to convert the array of numbers into the dictionary.

class Program
 {
   static void Main(string[] args)
   {
     int[] numbers = { 3, 6, 9, 12, 15 };
     
     //Linq Query
     var result_Linq = (from p in numbers select p).ToDictionary(p => p % 10);
     
     //Lambda Expression
     var result_Lambda = numbers.Select(p=>p).ToDictionary(p => p % 10);
     
     //Display Linq Query Result
     Console.WriteLine("Numbers in Dictionary (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine($"Key : {item.Key} , Value : {item.Value}");
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Numbers in Dictionary (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine($"Key : {item.Key} , Value : {item.Value}");
     }
     
     Console.Read();
   }
 }

Output

Numbers in Dictionary (Using Linq):
Key : 3 , Value : 3
Key : 6 , Value : 6
Key : 9 , Value : 9
Key : 2 , Value : 12
Key : 5 , Value : 15
Numbers in Dictionary (Using Lambda):
Key : 3 , Value : 3
Key : 6 , Value : 6
Key : 9 , Value : 9
Key : 2 , Value : 12
Key : 5 , Value : 15


OFTYPE

OFTYPE operator is used to return the elements of a specific type from a collection. OfType keyword is used for this purpose. Below is the example in this category.

Query to get the element of type int.

class Program
 {
   static void Main(string[] args)
   {
     object[] numbers = { null, 1.0, "rahul", 7, "four", 4, "goel", 5.0 };
     
     //Linq Query
     var result_Linq = numbers.OfType<int>();
     
     //Lambda Expression
     var result_Lambda = numbers.OfType<int>();
     
     //Display Linq Query Result
     Console.WriteLine("Numbers of type int (Using Linq):");
     foreach (var item in result_Linq)
     {
       Console.WriteLine(item);
     }
     
     //Display Lambda Expression Result
     Console.WriteLine("Numbers of type int (Using Lambda):");
     foreach (var item in result_Lambda)
     {
       Console.WriteLine(item);
     }
     
     Console.Read();
   }
 }

Output

Numbers of type int (Using Linq):
7
4
Numbers of type int (Using Lambda):
7
4