Converting Data Types (C#)

Conversion methods change the type of input objects.

Conversion operations in LINQ queries are useful in various applications. Following are some examples:

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

The following examples in this article use the common data sources for this area:

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; }
}

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.

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);
}

See also