Filter a Data Source by Type - CSharp LINQ

CSharp examples for LINQ:where

Description

Filter a Data Source by Type

Demo Code


using System;//w w w . j  a v  a 2  s .  c om
using System.Collections.Generic;
using System.Linq;
using System.Text;

class MainClass
    {
        static void Main(string[] args)
        {
            IList<object> mixedData = createData();
            IEnumerable <string> stringData = mixedData.OfType<string>();
            foreach (string str in stringData)
            {
                Console.WriteLine(str);
            }
        }

        static IList<object> createData()
        {
            return new List<object>()
            {
                "this is a string",
                23,
                9.2,
                "this is another string"
            };
        }
    }

Result


Related Tutorials