OfType Operator in LINQ

OfType Operator in LINQ with Examples

In this article, I am going to discuss the OfType Operator in LINQ with examples. Please read our previous article before proceeding to this article where we discussed the where filtering operator in C# with some examples. The OfType Operator in C# belongs to the filtering category of LINQ operators. As part of this article, we are going to discuss the following pointers in detail.

  1. What is OfType Operator in LINQ?
  2. Examples using both Method and Query syntax.
  3. Difference between OfType and is Operator in C#.NET.
What is OfType Operator in LINQ?

The OfType Operator in LINQ is used to filter specific type data from a data source based on the data type we passed to this operator. For example, if we have a collection that stores both integer and string values and if we need to fetch either only the integer values or only the string values from that collection then we need to use the OfType operator.

Following is the signature of the OfType method. It is implemented as a generic type, so there are no overloaded versions available for this method. This method can take any type of data type and then fetch the specified data type values from the collection.

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

Let say, we have a collection of type object. As we know the object class is the superclass of all data types so we can store any type of values in it like below.

OfType Operator in LINQ

Now our requirement is to fetch all the integer values from the collection by ignoring the string values. We can achieve this very easily by using the OfType operator in C# like below.

Understanding OfType Operator in LINQ with examples

The complete code is given below.

OfType Operator Using Method syntax in C#.NET:

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<object> dataSource = new List<object>()
            {
                "Tom", "Mary", 50, "Prince", "Jack", 10, 20, 30, 40, "James"
            };
            
            List<int> intData = dataSource.OfType<int>().ToList();
            
            foreach (int number in intData)
            {
                Console.Write(number + " ");
            } 
            
            Console.ReadKey();
        }
    }
}

Output:

OfType Operator in C#

OfType Operator Using Query Syntax in C#.NET:

There is no such OfType operator available in query syntax. Instead “is” operator is available which we can use to filter data based on a type from a data source. In the following example, the collection contains both string and integer values and we need to fetch only the string values.

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<object> dataSource = new List<object>()
            {
                "Tom", "Mary", 50, "Prince", "Jack", 10, 20, 30, 40, "James"
            };

            var stringData = (from name in dataSource
                           where name is string
                           select name).ToList();

            foreach (string name in stringData)
            {
                Console.Write(name + " ");
            } 
            
            Console.ReadKey();
        }
    }
}

Output:

C# OfType Operator using LINQ

OfType and is Operator with a condition in C#.NET:

Let say, we want to retrieve all the names whose length is greater than 3 and all the integer number which is greater than 30. Here we will use OfType operator to retrieve all the names and “is” operator to retrieve all the integers along with the required conditions.

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<object> dataSource = new List<object>()
            {
                "Tom", "Mary", 50, "Prince", "Jack", 10, 20, 30, 40, "James"
            };

            //Using Method Syntax
            var intData = dataSource.OfType<int>().Where(num => num > 30).ToList();
            foreach (int number in intData)
            {
                Console.Write(number + " ");
            }
            Console.WriteLine();

            //Using Qyery Syntax
            var stringData = (from name in dataSource
                           where name is string && name.ToString().Length > 3
                           select name).ToList();

            foreach (string name in stringData)
            {
                Console.Write(name + " ");
            } 
            
            Console.ReadKey();
        }
    }
}

Output:

OfType Operator

In the next article, I am going to discuss the SET Operators in LINQ with examples. I hope this article gives you a very good understanding of the concept LINQ OfType Operator in C#.

Leave a Reply

Your email address will not be published. Required fields are marked *