Friday, 6 July 2018

009 LINQ OfType


OfType method is used to filter LINQ objects.OfType comes under System.Linq  namespace. OfType is Enumarable ,  ofTypeIEnumarable. When ofType is applied on a query, the query Returns the result, that can be cast into particular supplied type.Object that cannot be cast into the supplied type will be discarded automatically.ofType a generic method, it takes type as parameter, that has to be filtered. 
 
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:
*************List of String object*************
India
USA
Pakistan
South Afrika
Russia
*************List of Integer object*************
12
13
15
*************List of Decimal object*************
False
True
True

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...