Conversion Operators in LINQ

by Sachin Singh

Posted on Saturday, 06 February 2021

Tags: Conversion Operators in LINQ

There are several Types of collections available in .Net , which can be categorized as shown below

Indexed Based collections

Indexed based collections are those where elements can be retrieved by providing their Index, for example


Key Based collections

key based collections are those which store items in a key value pair so that elements could be retrieved by providing their key , for example


Arrays are not flexible, this means Arrays have fixed length, but they are strongly typed, strongly typed means all elements in an array should be of the same data type, being a strongly typed collection it avoids boxing and unboxing so is faster than ArrayList.

On the other hand, ArrayList is flexible means it can store a variable number of items but are not strongly typed means an ArrayList stores values as an object, so when we retrieve an item from an ArrayList we need to perform the casting, due to such boxing and unboxing, it is slower than Array.

Generic collections

In reality, we need both qualities in a collection, we want a collection to be both flexible and strongly typed at the same time, for this, we have generic collections , when the concept of Generic has been applied to Indexed-based collections Like Array and ArrayList, we got List<T> generic List, which is both, flexible and Strongly typed, because in a List we can store variable numbers of Items but the items must be of a similar type, similarly when the concept of Generics are applied to Key-Based collections like HashTable, we get Dictionary. A Dictionary stores items in a key-value pair but are strongly typed. This means if we declare a Dictionary as Dictionary<string,int> then it will store element of integer type and their key must be a string type.

Based on Filtering criteria

In an IEnumerable<T> collections, filtering happens on the client-side whereas on IQueryable<T> collections, filtering happens at the server-side, this means, if we apply a Where() operator on an IEnumerable collection then SQL Server returns unfiltered items and Filtering is performed on the in-memory collection, whereas if we apply Where Operator on an IQueryable collection then SQL Server returns filtered data.

we now know that each collection has its own qualities, due to this LINQ provides us Conversion Operators, so that we can convert one type of collection into another type as per our requirement.

In LINQ, conversion operators are used to convert the type of elements in the list or collection. Linq provides us different types of conversion operators , as listed below
    • TOLIST
    • CAST
    • OFTYPE

By using these conversion operators we can convert the type of elements in the list, which means a list of integers can be converted into an array of Integers and so on.

The following diagram shows more detailed information related to conversion operators.

Linq Conversion Operators
Conversion operators in LINQ

Each of these conversion operators will be discussed in later articles in a great detail.