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



Question:

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?


Solution:1

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;  


Solution:2

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.


Solution:3

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

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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »