how to use LINQ Conversion Operators in C#?

how to use LINQ Conversion Operators (ToList, ToArray, ToLookup, Cast, OfType) in SQL data and normal data?

Last updated:9/24/2021 1:31:00 AM

1 Answers

Ravi Vishwakarma
Ravi Vishwakarma

Conversion Operators are used to converting elements in a list or collection. There are many types of conversion operators in LINQ.

There is the following Conversion Operator.

  1. ToList                          - it converts collection to list format
  2. ToArray                       - it converts collection to array format
  3. ToLookup                   - It converts group of elements into Lookup<tkey, telement>
  4. CAST                           - It converts non-generic list to generic list (IEnumerable to IEnumerable)
  5. OFTYPE                       - It is used to filters collection based on specified type
  6. ASENUMERABLE      - It is used to convert input elements as IEnumerable
  7. ASQUERYABLE         - It converts IEnumerable to IQueryable
  8. ToDictionary              - It converts input elements into dictionary based on key selector function

Example

using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
 public static void Main()
 {
  List<Student> students = new List<Student>()
  {
  new Student() { StudentId = 1, Name = 'Ashu', Marks = 500 },
  new Student() { StudentId = 2, Name = 'Shyam', Marks = 300 },
  new Student() { StudentId = 3, Name = 'Shriyam', Marks = 400 },
  new Student() { StudentId = 4, Name = 'Sunny', Marks = 550 },
  new Student() { StudentId = 5, Name = 'Ram', Marks = 600 },
  new Student() { StudentId = 6, Name = 'Krishna', Marks = 700 },
  new Student() { StudentId = 7, Name = 'Anupam', Marks = 550 }
  };
  List<Student> list = students.Where( stu => stu.Marks > 500).ToList();
  Console.WriteLine('\nToList() :\n');
  Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
  list.ForEach(item => Console.WriteLine('{0,-10} {1,10} {2,5}', item.Name, item.StudentId, item.Marks) );
  Student[] studentAry = students.Where( stu => stu.Name.StartsWith('A')).ToArray();
  Console.WriteLine('\nToArray() :\n');
  Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
  foreach(Student stu in studentAry)
   Console.WriteLine('{0,-10} {1,10} {2,5}', stu.Name, stu.StudentId, stu.Marks);
  IEnumerable<Student> enumerable = students.Where( stu => stu.Marks < 500).AsEnumerable();
  Console.WriteLine('\nAsEnumerable() :\n');
  Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
  foreach( Student student in enumerable)
   Console.WriteLine('{0,-10} {1,10} {2,5}', student.Name, student.StudentId, student.Marks);
  IQueryable<Student> queryable = students.Where( stu => stu.StudentId < 5).AsQueryable();
  Console.WriteLine('\nAsQueryable() :\n');
  Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');
  foreach( Student student in queryable)
   Console.WriteLine('{0,-10} {1,10} {2,5}', student.Name, student.StudentId, student.Marks);
  Dictionary<int, string> dictionary = students.Where( stu => stu.StudentId < 5).ToDictionary(stu => stu.StudentId, stu => stu.Name);
  Console.WriteLine('\nToDictionary() :\n');
  Console.WriteLine('{0,-3} {1,-10}','ID', 'Marks');
  foreach( var student in dictionary)
   Console.WriteLine('{0,-3} {1,-10}', student.Key, student.Value);
  Console.ReadLine();
 }
}
class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public int Marks { get; set; }
}

OUtput

ToList() :

Name ID Marks
Sunny 4 550
Ram 5 600
Krishna 6 700
Anupam 7 550
ToArray() :

Name ID Marks
Ashu 1 500
Anupam 7 550
AsEnumerable() :
Name ID Marks
Shyam 2 300
Shriyam 3 400
AsQueryable() :

Name ID Marks
Ashu 1 500
Shyam 2 300
Shriyam 3 400
Sunny 4 550
ToDictionary() :
ID  Marks     
1   Ashu      
2   Shyam     
3   Shriyam   
4   Sunny 

Answer