In this post we are going to discuss on how we can convert IEnumerable type to different type (It can be either a List or Dictionary or Array).
As we discussed in all
our previous post of LINQ series, LINQ query either returns anonymous type(In the case of Select clause while using projections) or it returns type of IEnumerable or IQueryable. But sometime we require our IEnumerable data to be converted to other types like List or Dictionary or Array. In order to do, we have some
extension methods, which will convert the IEnumerable type to the before said types. Let’s actually see them in action.
Converting IEnumerable Type to List
In order to convert IEnumerable type to List, use
ToList() extension method available.
Let’s take our same example which we have been dealing with this series so far.
//Create some students
List students = new List()
{
new Student { StudID = 101, Name= "Karthik", Class = "10th", Subjects = new List { "C#", "ADO.Net", "EF"}},
new Student { StudID = 108, Name= "Karthik", Class = "10th", Subjects = new List { "C#", "ADO.Net", "EF"}},
new Student { StudID = 104, Name = "Ram", Class = "11th", Subjects= new List{ ".Net", "C#", "SQL", "EF"}},
new Student { StudID = 103, Name = "John", Class = "9th", Subjects= new List{ ".Net", "MVC"}},
new Student { StudID = 106, Name = "Bill", Class = "9th", Subjects= new List{ "EF", "MVC"}},
};
Let’s query all the students and order them to descending by StudID and convert it to List
//Convert to List
List<Student,> studs = (from student in students
orderby student.StudID descending
select student).ToList();
foreach (Student item in studs)
Console.WriteLine(item.StudID + "\t" + item.Name);
As you can see above, we are using ToList() method to convert IEnumerable type to List. Then return value is of type List<Student>. We can then iterate all the elements using foreach loop as shown above.
Converting IEnumerable Type to Dictionary
Similar to List, we can convert IEnumerable type to Dictionary using ToDictionary() method as shown below
//Convert to Dictionary
Dictionary<int, string> studDict = (from student in students
orderby student.StudID descending
select student).ToDictionary(x => x.StudID, y => y.Name);
foreach (KeyValuePair<int, string> item in studDict)
Console.WriteLine(item.Key + "\t" + item.Value);
Since Dictionary collection has Key and Value pair, we need to specify the key and value in ToDictionary method, here I have assigned the key as StudID and value as Name.
I have iterated the Dictionary in foreach loop, note I have used
KeyValuePair<int, string> since that’s the type of Dictionary and then got all the key and value out from the collection.
Output
The output of both the above code looks like one shown below
Converting IEnumerable Type to Array
Here is the code
//Convert to Array
Student[] studs = (from student in students
orderby student.StudID descending
select student).ToArray();
foreach (Student item in studs)
Console.WriteLine(item.StudID + "\t" + item.Name);
Here is the complete code
public static void ConvertToDifferentTypeInLINQ()
{
//Create some students
List students = new List()
{
new Student { StudID = 101, Name= "Karthik", Class = "10th", Subjects = new List { "C#", "ADO.Net", "EF"}},
new Student { StudID = 108, Name= "Karthik", Class = "10th", Subjects = new List { "C#", "ADO.Net", "EF"}},
new Student { StudID = 104, Name = "Ram", Class = "11th", Subjects= new List{ ".Net", "C#", "SQL", "EF"}},
new Student { StudID = 103, Name = "John", Class = "9th", Subjects= new List{ ".Net", "MVC"}},
new Student { StudID = 106, Name = "Bill", Class = "9th", Subjects= new List{ "EF", "MVC"}},
};
//Convert to List
List studs = (from student in students
orderby student.StudID descending
select student).ToList();
foreach (Student item in studs)
Console.WriteLine(item.StudID + "\t" + item.Name);
//Convert to Dictionary
Dictionary<int, string> studDict = (from student in students
orderby student.StudID descending
select student).ToDictionary(x => x.StudID, y => y.Name);
foreach (KeyValuePair<int, string> item in studDict)
Console.WriteLine(item.Key + "\t" + item.Value);
//Convert to Array
Student[] studsArray = (from student in students
orderby student.StudID descending
select student).ToArray();
foreach (Student item in studsArray)
Console.WriteLine(item.StudID + "\t" + item.Name);
}
These are the different types of conversion methods available in LINQ which convert IEnumerable type to different types.
Please leave your comments and thanks for reading !!!
Thanks,
Karthik KK