Converting Data Types (C#)
Conversion methods change the type of input objects.
Important
These samples use an System.Collections.Generic.IEnumerable<T> data source. Data sources based on System.Linq.IQueryProvider use System.Linq.IQueryable<T> data sources and expression trees. Expression trees have limitations on the allowed C# syntax. Furthermore, each IQueryProvider
data source, such as EF Core may impose more restrictions. Check the documentation for your data source.
Conversion operations in LINQ queries are useful in various applications. Following are some examples:
- The Enumerable.AsEnumerable method can be used to hide a type's custom implementation of a standard query operator.
- The Enumerable.OfType method can be used to enable non-parameterized collections for LINQ querying.
- The Enumerable.ToArray, Enumerable.ToDictionary, Enumerable.ToList, 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 don't 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 | More Information |
---|---|---|---|
AsEnumerable | Returns the input typed as IEnumerable<T>. | Not applicable. | Enumerable.AsEnumerable |
AsQueryable | Converts a (generic) IEnumerable to a (generic) IQueryable. | Not applicable. | Queryable.AsQueryable |
Cast | Casts the elements of a collection to a specified type. | Use an explicitly typed range variable. For example:from string str in words |
Enumerable.Cast Queryable.Cast |
OfType | Filters values, depending on their ability to be cast to a specified type. | Not applicable. | Enumerable.OfType Queryable.OfType |
ToArray | Converts a collection to an array. This method forces query execution. | Not applicable. | Enumerable.ToArray |
ToDictionary | Puts elements into a Dictionary<TKey,TValue> based on a key selector function. This method forces query execution. | Not applicable. | Enumerable.ToDictionary |
ToList | Converts a collection to a List<T>. This method forces query execution. | Not applicable. | Enumerable.ToList |
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. | Enumerable.ToLookup |
Note
The following examples in this article use the common data sources for this area.
Each Student
has a grade level, a primary department, and a series of scores. A Teacher
also has a City
property that identifies the campus where the teacher holds classes. A Department
has a name, and a reference to a Teacher
who serves as the department head.
You can find the example data set in the source repo.
public enum GradeLevel
{
FirstYear = 1,
SecondYear,
ThirdYear,
FourthYear
};
public class Student
{
public required string FirstName { get; init; }
public required string LastName { get; init; }
public required int ID { get; init; }
public required GradeLevel Year { get; init; }
public required List<int> Scores { get; init; }
public required int DepartmentID { get; init; }
}
public class Teacher
{
public required string First { get; init; }
public required string Last { get; init; }
public required int ID { get; init; }
public required string City { get; init; }
}
public class Department
{
public required string Name { get; init; }
public int ID { get; init; }
public required int TeacherID { get; init; }
}
Query Expression Syntax Example
The following code example uses an explicitly typed range variable to cast a type to a subtype before accessing a member that is available only on the subtype.
IEnumerable people = students;
var query = from Student student in students
where student.Year == GradeLevel.ThirdYear
select student;
foreach (Student student in query)
{
Console.WriteLine(student.FirstName);
}
The equivalent query can be expressed using method syntax as shown in the following example:
IEnumerable people = students;
var query = people
.Cast<Student>()
.Where(student => student.Year == GradeLevel.ThirdYear);
foreach (Student student in query)
{
Console.WriteLine(student.FirstName);
}