Conversion Operators in Linq
Lets learn about the Conversion Operators in LINQ. Do you know what are Conversion Operator in Linq? Here I have demonstrated about them and how they can be converted to any other form.
Here we will discuss about the Conversion Operators in LINQ. By Conversion Operator we mean that the results obtained from the Linq query can be converted to any other form. Suppose we want that our Linq query to give an output in array or list, so there is a possibility to convert the results in these form using Conversion Operators.
There are mainly four types of Conversion Operators in Linq:
1. ToArray
2. ToList
3. ToDictionary
4. OfType
Now lets discuss all of them one by one.
1. ToArraypublic void ToArrayFunction()
{
int[] seq1 = { 6, 2, 3, 4, 1, 7 };
var sortedValues = from sorted in seq1
orderby sorted descending
select sorted;
var sortedValuesInArray = sortedValues.ToArray();
Console.WriteLine("Sorted Values in Array : ");
for (int i = 0; i < sortedValuesInArray.Length; i++)
{
Console.WriteLine(sortedValuesInArray[d]);
}
}
Output
Sorted Values in Array :
7
6
4
3
2
1
In the above example, first we are sorting the array, then the output is converted to an array using ToArray(). Unlike many standard query operators, this method does not use deferred execution.
2. ToListpublic void ToListFunction()
{
int[] seq1 = { 6, 2, 3, 4, 1, 7 };
var sortedValues = from sorted in seq1
orderby sorted descending
select sorted;
var sortedValuesInList = sortedValues.ToList();
Console.WriteLine("Sorted Values in List : ");
foreach (var w in sortedValuesInList )
{
Console.WriteLine(w);
}
}
Output
Sorted Values in List :
7
6
4
3
2
1
Similarly we can convert the output obtained to a list using ToList(). Now you may ask me whats the difference between ToArray() and ToList() and which one works better. So here is the answer
1. The key difference is that the return value of a ToList() is a generic List
2. You have to choose between ToArray() and ToList(), i.e. you need a fixed size array or a dynamic list in your application.
3. ToDictionarypublic void ToDictionaryFunction()
{
var marksOfStudent = new[] { new {Name = "Tom", Marks = 80},
new {Name = "John" , Marks = 75},
new {Name = "Cherry", Marks = 95}
};
var marksDictionary = marksOfStudent.ToDictionary(sr => sr.Name);
Console.WriteLine("1. Cherry's Marks: {0}", marksDictionary["Cherry"]);
Console.WriteLine("2. Tom's Marks: {0}", marksDictionary["Tom"]);
Console.WriteLine("3. John's Marks: {0}", marksDictionary["John"]);
}
Output
1. Cherry's Marks: { Name = Tom, Marks = 40 }
2. Tom's Marks: { Name = John, Marks = 40 }
3. John's Marks: { Name = Cherry, Marks = 40 }
ToDictionary() converts a collection into a Dictionary.We mainly used it to optimize performance—while retaining short and clear code. We used LINQ and the ToDictionary() extension method to quickly transform one kind of collection such as an array or List into a Dictionary collection.
4. OfTypepublic void ToOfTypeFunction()
{
object[] values = { null, 1, "two", 3, "four", 5, "six", 7.0, 6 };
var ints = values.OfType
Console.WriteLine("Numbers stored as Integers:");
foreach (var i in ints)
{
Console.WriteLine(i);
}
}
Output
Numbers stored as Integers:
1
3
5
6
In the above example, we use OfType() to return only the elements of the array that are of type Integer. So using this OfType() extension Method we can filter out only the required type according to our requirement.
Feedback
If you have any queries regarding Conversion Operators, please do ask me.