0

DataRowCollection : InternalDataCollectionBase

InternalDataCollectionBase : ICollection, IEnumerable

So , DataRowCollection indirectly inherites IEnumerable. Generally , If a class inherites IEnumerable we can apply linq operations . But in DataRowCollection this fails . Why?

Dhinesh
  • 45
  • 5

1 Answers1

2

Most of the Linq extension methods (from System.Linq.Enumerable) operate on a generic System.Collections.Generic.IEnumerable<T>, rather than the non-generic System.Collections.IEnumerable that InternalDataCollectionBase implements. That's why you use the Cast (or OfType) extension method to turn your IEnumerable into an IEnumerable<DataRow>.

Matthew King
  • 5,114
  • 4
  • 36
  • 50
  • I agree to this .Still, I have a few doubts. DataRowCollection is from COM dll (i.e) System.Data .To extend the functionality of COM dll to support linq ,System.Collections.IEnumerable is used .Is this correct ? – Dhinesh Nov 21 '13 at 06:01
  • I'm not quite sure what you're asking. DataRowCollection and System.Collections.IEnumerable have been around since .NET 1.1, but System.Collections.Generic.IEnumerable was only introduced in .NET 2.0, so DataRowCollection doesn't implement IEnumerable, only IEnumerable. Linq requires IEnumerable to be implemented, so doesn't work with DataRowCollection. Using Cast or OfType will give you an IEnumerable from an IEnumerable, so they can be used with DataRowCollection to give you an IEnumerable which can then be used with the Linq extension methods. – Matthew King Nov 21 '13 at 06:32