BlackWaspTM
LINQ
.NET 3.5+

LINQ Conversion

The thirteenth part of the LINQ to Objects tutorial describes some of the conversion operators provided by Language-Integrated Query (LINQ). These operators allow you to convert between sequence types or cast the elements of a sequence to another type.

Conversion

Language-Integrated Query (LINQ) provides several standard query operators that allow a sequence of items to be converted in some manner. These operators can be broadly categorised into two groups. The first group is used to convert an entire sequence into another type of collection without changing the types of the individual items. The second style of operator converts a collection into a sequence that implements the IEnumerable<T> interface at the same time as changing the underlying types of the individual items.

In this article we will look at the most common standard query operators for both types of conversion. Some conversion operators will be omitted from this tutorial and will be described in future articles. This article will not use query expression syntax as all conversion is achieved using standard query operators. No special query clauses are available for conversion but the operators may be applied to the results of a query.

Converting Enumerables to Other Types

We will begin by looking at the extension methods that convert an entire sequence from one type of collection to another, whilst leaving the contents unchanged. For simplicity we will use sample data that contains only basic types. However, there is no reason why more complex classes or structures cannot be used.

The sample data for this section is defined using the following code:

int[] array = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
List<int> list = new List<int> { 1, 4, 9, 16, 25, 36, 49, 64 };

ToArray

The first of the conversion operators is ToArray. As the name suggests, this method extracts all of the items from the source sequence and returns a new array containing those items. Unlike many standard query operators, this method does not use deferred execution. If used against the results of a query, the query will be immediately executed and the results copied into the new array. This can be useful when you want to ensure that a query is executed before other code has the opportunity to change the data source, or when you wish to generate a cached copy of a data set.

The following sample code creates an array containing the eight items from the list.

int[] converted = list.ToArray();

ToList

The ToList method is similar to ToArray in operation. The key difference is that the return value is a generic List<T> collection containing the items from the source sequence. This can be seen by executing the following example, which creates a list of integers containing the ten items from the sample array.

List<int> converted = array.ToList();
27 September 2010