How to Use LINQ OfType<>

LINQ OfType<> is a useful filtering function for returning items in a collection which match a specific type (or base type). An example might be for getting base items from a listbox of combobox.

An example without OfType:

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

An example of LINQ OfType:

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

GD Star Rating
loading...
GD Star Rating
loading...
  • Share/Bookmark

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

One Response to “How to Use LINQ OfType<>”

  1. Great Post!…

    [...] I found your entry interesting thus I’ve added a Trackback to it on my weblog :) [...]…

    GD Star Rating
    loading...
    GD Star Rating
    loading...

Leave a Reply