OfType Operator in LINQ

In LINQ, the OfType operator is used to filter a collection based on a specified type. This is useful when we have a collection that contains elements of different types, and we want to extract only those elements that belong to a specific type. In this lesson, we will explore the OfType operator in LINQ and learn how to use it to filter a collection based on a specified type in C#.

Syntax: The syntax of the OfType operator is as follows:
public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source);

where source is the input collection, and TResult is the type of the elements that we want to extract from the collection.

Example:

Let’s consider an example where we have a collection of objects that includes elements of different types. Suppose we want to extract only those elements that are of type string from the collection. We can use the OfType operator to achieve this. Here’s an example code snippet:

List<object> mixedList = new List<object> { "John", 123, "Jane", 456 };

IEnumerable<string> stringList = mixedList.OfType<string>();

foreach (var str in stringList)
{
    Console.WriteLine(str);
}

Output:

John
Jane

Explanation: In the above example, we have a List of objects called mixedList that contains elements of different types, including strings and integers. We apply the OfType operator to the mixedList collection and specify the type string as the type of elements that we want to extract from the collection. The OfType operator returns an IEnumerable<string> containing only the string elements from the mixedList collection. Finally, we iterate through the stringList collection using a foreach loop and print each element to the console.

Custom Type Example:

Let’s consider another example where we have a collection of different types of animals, and we want to extract only those animals that are of type Cat. Here’s an example code snippet:

class Animal
{
    public string Name { get; set; }
    public int Age { get; set; }
}

class Cat : Animal { }

class Dog : Animal { }

List<Animal> animalList = new List<Animal>
{
    new Cat { Name = "Tom", Age = 2 },
    new Dog { Name = "Spike", Age = 4 },
    new Cat { Name = "Garfield", Age = 6 }
};

IEnumerable<Cat> catList = animalList.OfType<Cat>();

foreach (var cat in catList)
{
    Console.WriteLine($"Name: {cat.Name}, Age: {cat.Age}");
}

Output:

Name: Tom, Age: 2
Name: Garfield, Age: 6
Explanation:

In the above example, we have a List of Animal objects called animalList that contains elements of different types, including Cat and Dog. We create a custom type called Cat that inherits from the Animal class. We apply the OfType operator to the animalList collection and specify the type Cat as the type of elements that we want to extract from the collection. The OfType operator returns an IEnumerable<Cat> containing only the Cat elements from the animalList collection. Finally, we iterate through the catList collection using a foreach loop and print the Name and Age properties of each Cat to the console.

Conclusion: The OfType operator in LINQ is a powerful feature that allows developers to filter a collection based on a specified type. By using the OfType operator, developers can extract only those elements that are of interest and eliminate other unwanted elements. The OfType operator is useful when working with collections that contain elements of different types, and we want to extract only those elements that belong to a specific type. We learned how to use the OfType operator in LINQ to filter a collection based on a specified type, and saw examples of using the operator with both built-in and custom types.

To summarize, the key points to remember about the OfType operator in LINQ are:
  • The OfType operator is used to filter a collection based on a specified type.
  • The input collection can contain elements of different types.
  • The OfType operator returns an IEnumerable<T> that contains only the elements of the specified type.
  • The OfType operator is useful when we want to extract only the elements of interest from a collection and eliminate other unwanted elements.

By mastering the OfType operator, developers can write more efficient and effective LINQ queries that help them accomplish their tasks more quickly and easily.