Queries Using TypeOf

This is something I’ve run across now and then. In some cases you may find someone who decides to stick a bunch of different types in a collection. Now I’m a big proponent of using strongly types object, collections, etc… However I know not everyone does that. Or there might be a situation where it didn’t happen that way. Here’s one of many ways that you can use a LINQ query to extract different kinds of data from an abstract collection.

TypeOfSolution TypeOfProgram

 

Program.cs Code Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Xml.Linq;
using System.Diagnostics;
using System.Collections;

namespace MyLinq
{
    class Program
    {
        ///
<summary>
        /// Demonstrating examples of working with the TypeOf operator combined with LINQ queries in various ways.
        /// </summary>

        /// <param name="args"></param>

        static void Main(string[] args)
        {
            typeOfExample();

            Console.ReadLine();
        }

        //Private Methods
        //
        private static void typeOfExample()
        {
            // Working with an array list that isn't strongly typed with objects of different types.
            ArrayList arrayList = new ArrayList();
            arrayList.Add("Dashing");
            arrayList.Add(new object());
            arrayList.Add(24);
            arrayList.Add("Employees");
            arrayList.Add(new Employee (1, "Daniel", 1, new DateTime(2009, 12, 22)));

            // Iterating thru the array list and looking for string object types.
            var stringQuery = from stringName in arrayList.OfType<string>()
                              select stringName;

            Console.WriteLine("****Displaying String Query Results****");
            write(stringQuery);

            // Iterate thru the array list and get the employee object.
            var employeeQuery = from employeeName in arrayList.OfType<Employee>()
                                select employeeName;

            Console.WriteLine("****Displaying Employee Object Query Results****");
            write(employeeQuery);
        }

        private static void write(IEnumerable<string> stringQuery)
        {
            foreach (var item in stringQuery)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();
        }

        private static void write(IEnumerable<Employee> employeeQuery)
        {
            foreach (var item in employeeQuery)
            {
                Console.WriteLine(item.Name);
            }
        }
    }
}

What I’ve done in this example is create a ArrayList with different types of objects in the collection. We’ve got strings, integers, and some object types all mixed in. By using the IEnumerable OfType method in my LINQ query I’m able to iterate thru the collection and pull out what I need. In this case I’m pulling out the string and Employee objects from the collection and displaying them.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s