ZetCode

C# LINQ OfType

last modified July 5, 2023

In this article we show how to filter elements of enumerables by type using LINQ's OfType method.

Language-Integrated Query (LINQ) is a domain-specific language for querying data from various data sources, including arrays, lists, XML files, or databases.

public static IEnumerable<TResult> OfType<TResult> (this IEnumerable source);

The OfType method filters the elements of an IEnumerable based on a specified type. The TResult is the type to filter the elements of the sequence on. The source is the sequence whose elements to filter.

C# LINQ OfType example

The following is a simple example with LINQ's OfType method.

Program.cs
object[] vals = new object[] {
    "falcon", 12, "sky", "new", "34", 11
};

var res = vals.OfType<int>();

foreach(var e in res)
{
    Console.WriteLine(e);
}

Console.WriteLine("-----------------------");

var res2 = vals.OfType<string>();

foreach(var e in res2)
{
    Console.WriteLine(e);
}

We define an array of objects. In the array we have strings and integers. With the help of the OfType method, we pick up integers and strings separately.

var res = vals.OfType<int>();

foreach(var e in res)
{
    Console.WriteLine(e);
}

Here we filter all integers from the array. The type is specified between the angle brackets.

$ dotnet run
12
11
-----------------------
falcon
sky
new
34

C# LINQ OfType example II

In the next example, we work with record types.

Program.cs
List<Person> persons = new List<Person> {
    new User("John Doe"), new Admin("Roger Roe"),
    new User("Peter Smith"), new User("Peter Novotny")
};

var res = from person in persons.OfType<Admin>()
          select person;

Console.WriteLine(string.Join(",", res));

Console.WriteLine("---------------------------");

var res2 = from person in persons.OfType<User>()
           select person;

Console.WriteLine(string.Join(",", res2));

interface Person { }
record User(string Name) : Person;
record Admin(string Name) : Person;

We define the Person interface and two record types: User and Admin. With LINQ query expression, we separate the admins and the users from the list of persons.

$ dotnet run
Admin { Name = Roger Roe }
---------------------------
User { Name = John Doe },User { Name = Peter Smith },User { Name = Peter Novotny }

C# LINQ Cast

There is a similar Cast method. The method casts the elements of an IEnumerable to the specified type. Unlike OfType, the Cast method throws InvalidCastExceptionif the element in the sequence cannot be cast to the given type.

Program.cs
object[] vals = new object[] {
    "falcon", 12, "sky", "new", "34", 11
};

var res = vals.Cast<string>();

foreach(var e in res)
{
    Console.WriteLine(e);
}

We use the Cast method on an array of objects.

$ dotnet run
falcon
Unhandled exception. System.InvalidCastException: Unable to cast object of type 
'System.Int32' to type 'System.String'.
...

Source

Enumerable.OfType method

In this article we have presented the LINQ OfType method.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all C# tutorials.