views:

46

answers:

3

Why are there no LINQ extension methods on RepeaterItemCollection despite the fact that it implements IEnumerable?

I'm using Linq to objects elsewhere in the same class. However, when I attempt to do so with the RepeaterItemCollection they aren't available. I was under the impression that the LINQ extension methods were available to classes that implement IEnumerable.

What am I missing?

+2  A: 

It would need to implement the generic IEnumerable<> to work with LINQ, which is does not.

You can, however, "cast" it first to the type you need with the Cast<> extension method.

Lucero
Yes, it looks like this class was born in .NET 1.0 so does not know generics.
AakashM
+4  A: 

It implements IEnumerable, but not IEnumerable<T>.

That doesn't mean you can't use it though - that's part of what OfType and Cast are for, building a generic sequence from a nongeneric one:

var filtered = items.Cast<RepeaterItem>()
                    .Where(...) // Or whatever
                    .ToList();

In this case Cast is more appropriate than OfType as you should be confident that it will only contain RepeaterItem values. Note that Cast is what gets used if you state the type of a range variable in a query expression, so this will work too:

var query = from RepeaterItem item in items
            where item.ItemType == ListItemType.SelectedItem
            select item.DataItem;
Jon Skeet
+1  A: 

According to MSDN, it has the following 4 extension methods:

  • AsParallel()
  • AsQueryable()
  • Cast<TResult>()
  • OfType<TResult>()
Eric