Converting Data Types
Conversion methods change the type of input objects.
Conversion operations in LINQ queries are useful in a variety of applications. Following are some examples:
The Enumerable.AsEnumerable<TSource> method can be used to hide a type's custom implementation of a standard query operator.
The Enumerable.OfType<TResult> method can be used to enable non-parameterized collections for LINQ querying.
The Enumerable.ToArray<TSource>, Enumerable.ToDictionary, Enumerable.ToList<TSource>, and Enumerable.ToLookup methods can be used to force immediate query execution instead of deferring it until the query is enumerated.
Methods
The following table lists the standard query operator methods that perform data-type conversions.
The conversion methods in this table whose names start with "As" change the static type of the source collection but do not enumerate it. The methods whose names start with "To" enumerate the source collection and put the items into the corresponding collection type.
Method Name |
Description |
C# Query Expression Syntax |
Visual Basic Query Expression Syntax |
More Information |
---|---|---|---|---|
AsEnumerable |
Returns the input typed as IEnumerable<T>. |
Not applicable. |
Not applicable. |
|
AsQueryable |
Converts a (generic) IEnumerable to a (generic) IQueryable. |
Not applicable. |
Not applicable. |
|
Cast |
Casts the elements of a collection to a specified type. |
Use an explicitly typed range variable. For example: from string str in words |
From … As … |
|
OfType |
Filters values, depending on their ability to be cast to a specified type. |
Not applicable. |
Not applicable. |
|
ToArray |
Converts a collection to an array. This method forces query execution. |
Not applicable. |
Not applicable. |
|
ToDictionary |
Puts elements into a Dictionary<TKey, TValue> based on a key selector function. This method forces query execution. |
Not applicable. |
Not applicable. |
|
ToList |
Converts a collection to a List<T>. This method forces query execution. |
Not applicable. |
Not applicable. |
|
ToLookup |
Puts elements into a Lookup<TKey, TElement> (a one-to-many dictionary) based on a key selector function. This method forces query execution. |
Not applicable. |
Not applicable. |
Query Expression Syntax Example
The following code example uses an explicitly-typed range variable in C# or the From As clause in Visual Basic to cast a type to a subtype before accessing a member that is available only on the subtype.
Class Plant
Public Property Name As String
End Class
Class CarnivorousPlant
Inherits Plant
Public Property TrapType As String
End Class
Sub Cast()
Dim plants() As Plant = {
New CarnivorousPlant With {.Name = "Venus Fly Trap", .TrapType = "Snap Trap"},
New CarnivorousPlant With {.Name = "Pitcher Plant", .TrapType = "Pitfall Trap"},
New CarnivorousPlant With {.Name = "Sundew", .TrapType = "Flypaper Trap"},
New CarnivorousPlant With {.Name = "Waterwheel Plant", .TrapType = "Snap Trap"}}
Dim query = From plant As CarnivorousPlant In plants
Where plant.TrapType = "Snap Trap"
Select plant
Dim sb As New System.Text.StringBuilder()
For Each plant In query
sb.AppendLine(plant.Name)
Next
' Display the results.
MsgBox(sb.ToString())
' This code produces the following output:
' Venus Fly Trap
' Waterwheel Plant
End Sub
class Plant
{
public string Name { get; set; }
}
class CarnivorousPlant : Plant
{
public string TrapType { get; set; }
}
static void Cast()
{
Plant[] plants = new Plant[] {
new CarnivorousPlant { Name = "Venus Fly Trap", TrapType = "Snap Trap" },
new CarnivorousPlant { Name = "Pitcher Plant", TrapType = "Pitfall Trap" },
new CarnivorousPlant { Name = "Sundew", TrapType = "Flypaper Trap" },
new CarnivorousPlant { Name = "Waterwheel Plant", TrapType = "Snap Trap" }
};
var query = from CarnivorousPlant cPlant in plants
where cPlant.TrapType == "Snap Trap"
select cPlant;
foreach (Plant plant in query)
Console.WriteLine(plant.Name);
/* This code produces the following output:
Venus Fly Trap
Waterwheel Plant
*/
}
See Also
Tasks
How to: Query an ArrayList with LINQ