LINQ (Language Integrated Query) is a powerful feature of the .NET framework that allows developers to query various data sources using a unified syntax. One of the useful operators in LINQ is the OfType
operator, which enables filtering of elements based on their type. In this article, we will explore the LINQ OfType Operator.
What is OfType Operator in LINQ?
The OfType
operator in LINQ is used to filter elements in a collection based on their type. It returns only the elements of the specified type and filters out any other elements. This operator is particularly useful when dealing with collections that contain multiple types.
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.
1 2 3 |
public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source); |
LINQ OfType Method in C#
Let’s say, we have a collection of type Objects. 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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
LINQ OfType Operator 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
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:
1 2 3 4 5 6 |
Using OfType Method 50 10 20 30 40 Using IS Operator Tom Mary Prince Jack James |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
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(); } } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
using System; using System.Collections.Generic; using System.Linq; namespace LINQDemo { 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(); } } } |
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.
Leave a Comment