OfType method is used to filter LINQ objects.OfType comes under System.Linq namespace. OfType is Enumarable , ofType
Here is the OfType example ,the list is filtered against
String , Decimal ,Integer
Eample
using System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
IList
myList = new
ArrayList();
myList.Add("India");
myList.Add(false);
myList.Add(12);
myList.Add(true);
myList.Add(13);
myList.Add("USA");
myList.Add("Pakistan");
myList.Add(15);
myList.Add(true);
myList.Add("South
Afrika");
myList.Add("Russia");
/********filtering string object*************/
var
strqery = from
s in
myList.OfType<string>()
select
s;
Console.WriteLine("*************List
of String object*************");
foreach
(object
c in
strqery)
{
Console.WriteLine(c.ToString());
}
/********filtering
integer object*************/
var
intqery = from
s in
myList.OfType<int>()
select
s;
Console.WriteLine("*************List
of Integer object*************");
foreach (object c in intqery)
{
Console.WriteLine(c.ToString());
}
/********filtering
decimal object*************/
Console.WriteLine("*************List
of Decimal object*************");
var
decqery = from
s in
myList.OfType<bool>()
select
s;
foreach
(object
c in
decqery)
{
Console.WriteLine(c.ToString());
}
Console.ReadKey();
}
}
}
Output:
Output:
*************List of String object*************
India
USA
Pakistan
South Afrika
Russia
*************List of Integer object*************
12
13
15
*************List of Decimal object*************
False
True
True
India
USA
Pakistan
South Afrika
Russia
*************List of Integer object*************
12
13
15
*************List of Decimal object*************
False
True
True