.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> LINQ >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

LINQ: OfType

Posted By:Syed Shakeer Hussain       Posted Date: December 17, 2011    Points: 40    Category: LINQ    URL: http://www.dotnetspark.com  

The OfType(IEnumerable) method returns only those elements in source that can be cast to type TResult.
 

In this article you can learn how to filter the dataType containing from different data Types 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace LinQSamples
{
    class Program
    {
        static void Main(string[] args)
        {
           
      object[] data = { "Shakeer", 1, 3, "Hussain","DotNetSpark.Com",35.05 };
            IEnumerable Strquery = data.OfType();
            IEnumerable intQuery = data.OfType();
            IEnumerable floatQuery = data.OfType();

                        
            Console.WriteLine("Retreiving String Data");
            Console.WriteLine("------------------------");
            foreach (string str in Strquery)
            {
                Console.WriteLine(str);
            }
 
            Console.WriteLine("\nRetreiving Numeric Data");
            Console.WriteLine("------------------------");
            foreach (int  Numeric in intQuery)
            {
                Console.WriteLine(Numeric);
            }
 
            Console.WriteLine("\nRetreiving Double Data");
            Console.WriteLine("------------------------");
            foreach (double floatdata in floatQuery)
            {
                Console.WriteLine(floatdata);
            }

             }
    }
}

OutPut:

Retreiving String Data
-----------------------
Shakeer
Hussain
DotNetSpark.Com

Retreiving Numeric Data
-----------------------
1
3

Retreiving Double Data
--------------------------
35.05


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend