;

Filtering Operator - OfType


In LINQ, Filtering operators are used to filter the data from the collection based on the given criteria. In this tutorial, we will learn about the filtering operator - OfType.

OfType

In LINQ, The OfType Operator is used to filter specific type data from a collection or data source based on the data type we passed to this operator. 

For example, if we have a collection that contains both string type values and integer type values and you need to fetch only string type values from the collection then you can use the OfType operator.

This is implemented as a generic type and there are no overload methods of OfType Operator.

Examples of Filtering Operator - OfType 

Example 1: OfType Operator using Query Syntax

Example 1: OfType Operator using Query Syntax
using System;
using System.Linq;
using System.Collections;

public class Program
{
    public static void Main()
    {
        IList mixedList = new ArrayList();
        mixedList.Add(0);
        mixedList.Add("One");
        mixedList.Add("Two");
        mixedList.Add(3);
        mixedList.Add(new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000 });

        var stringResult = from s in mixedList.OfType<string>()
                           select s;

        var intResult = from s in mixedList.OfType<int>()
                        select s;

        var empResult = from s in mixedList.OfType<Employee>()
                        select s;

        foreach (var str in stringResult)
            Console.WriteLine(str);

        foreach (var integer in intResult)
            Console.WriteLine(integer);

        foreach (var emp in empResult)
            Console.WriteLine(emp.EmpName);

        Console.ReadKey();
    }

}

public class Employee
{

    public int EmpID { get; set; }
    public string EmpName { get; set; }
    public string City { get; set; }
    public int Salary { get; set; }

}
Output
One
Two
0
3
John

Example 2: OfType Operator Using Method Syntax

Example 2: OfType Operator Using Method Syntax
using System;
using System.Linq;
using System.Collections;

public class Program
{
    public static void Main()
    {
        IList mixedList = new ArrayList();
        mixedList.Add(0);
        mixedList.Add("One");
        mixedList.Add("Two");
        mixedList.Add(3);
        mixedList.Add(new Employee() { EmpID = 1, EmpName = "John", City = "New York", Salary = 130000 });

        var stringResult = mixedList.OfType<string>();

        var intResult = mixedList.OfType<int>();

        var empResult = mixedList.OfType<Employee>();

        foreach (var str in stringResult)
            Console.WriteLine(str);

        foreach (var integer in intResult)
            Console.WriteLine(integer);

        foreach (var emp in empResult)
            Console.WriteLine(emp.EmpName);

        Console.ReadKey();
    }

}

public class Employee
{

    public int EmpID { get; set; }
    public string EmpName { get; set; }
    public string City { get; set; }
    public int Salary { get; set; }

}
Output
One
Two
0
3
John

Learn Sorting Operator - OrderBy & OrderByDescending in the next tutorial