Welcome to Manicprogrammer Sign in | Join | Help

Linq OfType<T> expression

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.

Published Wednesday, April 15, 2009 1:08 PM by willeke
Filed under: , ,

Comments

No Comments
Anonymous comments are disabled