Rediscovering the Obvious

…stumbling in the footsteps of greatness

Linq OfType<T> expression

without comments

I just discovered a function in Linq that will save me a bit of typing and allow me to better express myself.

For a long while, I’ve written this:

ColorWrapper x = combo.Items.Where( obj => obj is ColorWrapper ).Cast< ColorWrapper >().FirstOrDefault(cw => cw.Color == value);

Today, I realized that the framework designers were ahead of me, and provided OfType<T>, which does the same thing as follows;

ColorWrapper y = combo.Items.OfType<ColorWrapper>().FirstOrDefault(cw => cw.Color == value);

 Lesson Learned: Pay more attention to ALL of the aspects offered by a new technology, they all have their appropriate uses.

Written by erwilleke

April 15th, 2009 at 1:08 pm

Posted in Uncategorized

Leave a Reply