LINQ | Filtering Operator | OfType
Filtering operators are those operators which are used to filter the data according to the user requirement from the given data source or from the given sequence. For example, in an employee record, we want to get the data of the employees whose age in 21. So, we filter the record, according to their age. In LINQ, you can filter using the following operators:
- Where
- OfType
OfType Operator
OfType operator filters the sequence or data source depends upon their ability to cast an element in a collection to a specified type. It is implemented by using deferred execution, means it immediately returns an object which contains the information that is required to perform the action, but executes when the object is iterate over the loop by using foreach loop or for loop.
- In LINQ, you are allowed to use multiple OfType in the single query.
- It does not support query syntax in C# or VB.Net language, but you can use it with syntax as shown in the below example.
Example:
// C# program to illustrate the
// concept of OfType operator
using
System;
using
System.Linq;
using
System.Collections;
// Employee details
public
class
Employee {
public
int
emp_id
{
get
;
set
;
}
public
string
emp_name
{
get
;
set
;
}
public
string
emp_gender
{
get
;
set
;
}
public
string
emp_hire_date
{
get
;
set
;
}
public
int
emp_salary
{
get
;
set
;
}
}
public
class
GFG {
// Main method
static
public
void
Main()
{
// Data Source
ArrayList myarray =
new
ArrayList();
myarray.Add(
"GeeksforGeeks"
);
myarray.Add(23);
myarray.Add(
new
Employee() {emp_id = 209, emp_name =
"Anjita"
});
myarray.Add(
new
Employee() {emp_id = 210, emp_name =
"Soniya"
});
myarray.Add(
new
Employee() {emp_id = 300, emp_name =
"Rohan"
});
// Using OfType operator
var
res1 =
from
e1
in
myarray.OfType<
string
>()
select
e1;
foreach
(
var
val1
in
res1)
{
Console.WriteLine(val1);
}
var
res2 =
from
e2
in
myarray.OfType<Employee>()
select
e2;
foreach
(
var
val2
in
res2)
{
Console.WriteLine(val2.emp_name);
}
}
}
Output:GeeksforGeeks Anjita Soniya Rohan
- It supports method syntax in C# or in VB. It available in both the Queryable and Enumerable class. As shown in the below example.
Example:
// C# program to illustrate the
// concept of OfType operator
using
System;
using
System.Linq;
using
System.Collections;
// Employee details
public
class
Employee {
public
int
emp_id
{
get
;
set
;
}
public
string
emp_name
{
get
;
set
;
}
public
string
emp_gender
{
get
;
set
;
}
public
string
emp_hire_date
{
get
;
set
;
}
public
int
emp_salary
{
get
;
set
;
}
}
class
GFG {
// Main method
static
public
void
Main()
{
// Data Source
ArrayList myarray =
new
ArrayList();
myarray.Add(
new
Employee() {emp_id = 209, emp_name =
"Anjita"
});
myarray.Add(
new
Employee() {emp_id = 210, emp_name =
"Soniya"
});
myarray.Add(
new
Employee() {emp_id = 300, emp_name =
"Rohan"
});
// Get the id of the employees
// Using OfType operator
var
res = myarray.OfType<Employee>();
foreach
(
var
val
in
res)
{
Console.WriteLine(
"Employee Id: {0}"
, val.emp_id);
}
}
}
Output:Employee Id: 209 Employee Id: 210 Employee Id: 300
Please Login to comment...