Home Contact

Timmy Kokke

…just sorting my bubbles…

News




Timmy Kokke's Blog

↑ Grab this Headline Animator

Timmy Kokke at Blogged

Twitter












Tag Cloud


Archives

Post Categories

Image Galleries

Silverlight

Syndication:

Getting items of a type from a collection using LINQ

There are a lot of situations in Silverlight and WPF where you would like to get a list of items from a collection of a certain type. For example all textboxes in a Grid, or all customer records from a ListView.

 

LINQ provides us with an extension method to do this very easily, the OfType<T>() extension on IEnumerable:

public static IEnumerable<TResult> OfType<TResult>( 
    this IEnumerable source 
)

Let me explain the use of this method thru some examples.

 

Say you have a grid with some textboxes, buttons and labels and you want to iterate thru the textboxes to perform some calculation. Instead of creating a foreach loop, looping thru all children of this grid and checking if this child is a textbox, let LINQ figure this out. This is how you do this:

foreach(TextBox tb in LayoutRoot.Children.OfType<TextBox>()){
     //Do some stuff
}

The same thing can be done with a ListView. In this ListView there are Customers and Employees mixed together. To provide easy access to only the Customers or Employees I created readonly properties to do this.

List<Customer> Customers
{
    get
    {
        return aList.Items.OfType<Customer>().ToList();
    }
}
List<Employee> Employees
{
    get
    {
        return aList.Items.OfType<Employee>().ToList();
    }
}

Now you have an easy access to only the customers or employees in this ListView “aList”.


Feedback

# re: Getting items of a type from a collection using LINQ

Interesting. I've been using LINQ a while now, and I hadn't noticed OfType. Very useful.

Have you done any performance profiling on it yet? 12/22/2008 5:29 PM | Randolpho

# re: Getting items of a type from a collection using LINQ

I stumbled upon this a few months ago. Until than I always used LINQ with a where-clause looking at the type. I havn't done any performance profiling yet, but I can't imagine it's slower than a where-clause. 12/22/2008 7:03 PM | Timmy Kokke

# re: Getting items of a type from a collection using LINQ

I'd not come across this either. I ran some performance tests using the two methods below. Running 100 iterations of each against 1,000,000 _things with an equal split of AThing and BThing takes about 18 seconds for the OfType version against 14 seconds for the Where version.

private static void ActionWhere()
{
_athings.Clear();
foreach (AThing thing in _things.Where(thing => thing.GetType() == typeof(AThing)))
_athings.Add(thing);

_bthings.Clear();
foreach (BThing thing in _things.Where(thing => thing.GetType() == typeof(BThing)))
_bthings.Add(thing);
}

private static void ActionOfType()
{
_athings.Clear();
foreach (AThing thing in _things.OfType<AThing>())
_athings.Add(thing);

_bthings.Clear();
foreach (BThing thing in _things.OfType<BThing>())
_bthings.Add(thing);
} 4/2/2009 4:01 PM | Steve Crane

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: