Loading

Microsoft .Net 6 LINQ

What is OfType Operator in LINQ ?. The Complete Microsoft .Net 6 LINQ Developer Course 2022 [Videos].

In this Video, I am going to discuss the OfType Operator in LINQ with examples. Please watch our previous Video before proceeding to this Video 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 Video, 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

See All

Comments (461 Comments)

Submit Your Comment

See All Posts

Related Posts

Microsoft .Net 6 LINQ / Blog

What is Microsoft .Net 6 LINQ?

LINQ stands for Language-Integrated Query and it is a powerful query language that was introduced with .Net 3.5 & Visual Studio 2008. You can use LINQ with C# or VB to query different types of data sources such as SQL, XML, In memory objects, etc.
14-Feb-2022 /38 /461

Microsoft .Net 6 LINQ / Blog

What is Architecture of LINQ?

In this article, I am going to discuss the Architecture of LINQ. The term LINQ stands for Language Integrated Query and it is pronounced as LINK. Nowadays the use of use LINQ increasing rapidly. So, as a developer, you should understand the Linq and its architecture. At the end of this article, you will have a very good understanding of the following pointers.
14-Feb-2022 /38 /461

Microsoft .Net 6 LINQ / Blog

How to write Different Ways to Write LINQ Query?

In this article, I am going to discuss the Different Ways to write LINQ Query i.e. Linq Query Syntax and Linq Method Syntax with examples. Please read our previous article where we discussed the Architecture of LINQ i.e. how LINQ works. In this article, we are going to discuss the following pointers.
14-Feb-2022 /38 /461