LINQ Conversion Operators (ToList, ToArray, ToLookup, Cast, OfType)

In LINQ, conversion operators are useful to convert the type of elements in the list/collection. In LINQ, we have different types of conversion operators available those are 

 

  1. TOLIST
  2. TOARRAY
  3. TOLOOKUP
  4. CAST
  5. OFTYPE
  6. ASENUMERABLE
  7. ASQUERYABLE

Using these conversion operators, we can convert the type of elements in the list.

 

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

 

OperatorDescription
ToList It converts a collection to List
ToArray It converts a collection to an array
ToLookUp It converts a group of elements into Lookup<tkey, telement>
Cast It converts non-generic list to generic list (IEnumerable to IEnumerable)
OfType It is useful to filter collection based on a specified type
AsEnumerable It is useful to convert input elements as IEnumerable
AsQueryable It converts IEnumerable to IQueryable
ToDictionary It converts input elements into a dictionary based on the key selector function.

In the coming chapters, we will learn each conversion operator with examples.