Back to: LINQ Tutorial For Beginners and Professionals
LINQ OfType Operator in C# with Examples
In this article, I am going to discuss the LINQ OfType Operator in C# with Examples. Please read our previous article before proceeding to this article where we discussed the Where Filtering Operator in C# with Examples. The OfType Operator belongs to the filtering category of LINQ operators. As part of this article, we are going to discuss the following pointers in detail.
- What is OfType Operator in LINQ?
- Examples using both Method and Query syntax.
- 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 string values from that collection then we need to use the LINQ OfType Operator.
Following is the signature of the LINQ 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’s 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.
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 LINQ OfType Method in C# as follows.
Example to Understand LINQ OfType Method Using Method Syntax in C#:
In the below example, the dataSource collection contains both integer and string data. Then using the OfType<int>() method we are fetching only the integer data from the dataSource using the LINQ Method syntax.
using System; using System.Collections.Generic; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { //Data Source Contains both Integer and String Data List<object> dataSource = new List<object>() { "Tom", "Mary", 50, "Prince", "Jack", 10, 20, 30, 40, "James" }; //Fetching only the Integer Data from the Data Source //using Linq Method Syntax and OfType Method List<int> intData = dataSource.OfType<int>().ToList(); //Print the Integer Data foreach (int number in intData) { Console.Write(number + " "); } Console.ReadKey(); } } }
Output: 50 10 20 30 40
Example to Understand LINQ OfType Operator Using Query Syntax in C#:
We can achieve this using two ways i.e. using OfType Method and IS Operator. In the following example, the collection contains both string and integer values and we are only fetching the string values using both OfType method and IS Operator with the Query syntax.
using System; using System.Collections.Generic; using System.Linq; namespace LINQDemo { class Program { static void Main(string[] args) { //Data Source Contains both Integer and String Data List<object> dataSource = new List<object>() { "Tom", "Mary", 50, "Prince", "Jack", 10, 20, 30, 40, "James" }; //Fetching only the Integer Data from the Data Source //using Linq Query Syntax and OfType Method var intData = (from num in dataSource.OfType<int>() select num).ToList(); //Print the Integer Data Console.WriteLine("Using OfType Method"); foreach (int number in intData) { Console.Write(number + " "); } Console.WriteLine("\nUsing IS Operator"); //Fetching only the String Data from the Data Source //using Linq Query Syntax and is Method var stringData = (from name in dataSource where name is string select name).ToList(); //Print the Integer Data foreach (string name in stringData) { Console.Write(name + " "); } Console.ReadKey(); } } }
Output:
OfType and IS Operators with a Condition in C#:
Let’s 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 the OfType Method to retrieve all the integers which are greater than 30 and the “is” operator to retrieve all the strings with lengths greater than 3 characters.
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" }; //Fetching the Integer Numbers which are greater than 30 //Using Method Syntax var intData = dataSource.OfType<int>().Where(num => num > 30).ToList(); foreach (int number in intData) { Console.Write(number + " "); } Console.WriteLine(); //Fetching the String Names whose length is greater than 3 characters //Using Query Syntax with is Operator var stringData = (from name in dataSource where name is string && name.ToString().Length > 3 select name).ToList(); //Fetching the String Names whose length is greater than 3 characters //Using Query Syntax with OfType Operator var stringData2 = (from name in dataSource.OfType<string>() where name.Length > 3 select name).ToList(); foreach (string name in stringData2) { Console.Write(name + " "); } Console.ReadKey(); } } }
Output:
OfType Operator in C# with Where Condition:
Now, let us see how we can use the OfType in C# with both Method and Query Syntax with the Where Condition. In the below example, we are retrieving all the numbers which are greater than 30 using both Method and Query Syntax using the OfType operator.
using System; using System.Collections.Generic; using System.Linq; namespace LINQOfTypeDemo { class Program { static void Main(string[] args) { //Data Source which contains both String and Integer Data List<object> dataSource = new List<object>() { "Tom", "Mary", 50, "Prince", "Jack", 10, 20, 30, 40, "James" }; //Fetching the Integer numbers from the Data Source where the Number is Greater than 30 //Using Method Syntax var intData = dataSource.OfType<int>().Where(num => num > 30).ToList(); foreach (int number in intData) { Console.Write(number + " "); } Console.WriteLine(); //Using Query Syntax with OfType Operator var intData1 = (from num in dataSource.OfType<int>() where num > 30 select num).ToList(); //Using Query Syntax with is Operator //Here, we need to type cast num to int before applying the > operator var intData2 = (from num in dataSource where num is int && (int)num > 30 select num).ToList(); foreach (int name in intData2) { Console.Write(name + " "); } Console.ReadKey(); } } }
Output:
Note: In LINQ, the Where Extension Method is used to filter the data source based on a condition i.e. predicate function. The OfType Extension Method is used to filter the data source based on a given type. And we can use both Where and OfType Methods multiple times in a single LINQ Query.
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 of LINQ OfType Operator in C# with Examples.
Hi Team,
Thanks for all the efforts you are putting to provide invaluable study material for us.
Much much appreciated.
Could you please re-write the query syntax as its providing only string in the data, Because we have missed to add the integer condition where value should be greater than 30.
Hi
Thanks for your feedback. We have updated the example.
Thanks for your quick reply. Appreciated.