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:
using System;
using System.Linq;
using System.Collections;
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 {
static public void Main()
{
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" });
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:
using System;
using System.Linq;
using System.Collections;
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 {
static public void Main()
{
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" });
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!