I found that it is not possible to directly use LINQ over an instance of IEnumerable<T>
i.e. System.Data.DataRowCollection
This class derives from the class System.Data.InternalDataCollectionBase
which implements IEnumerable<T>
, so it makes completely sense to being able to use LINQ functions over System.Data.DataRowCollection
.... but I can't.
Check the following example:
using (DBConnection connExport = DBConnection.OpenConnection())
{
DataTable dt = GetDataTableFromDatabase();
DataRow dr1 = dt.Rows.First(); // This LINQ is not allowed
foreach(DataRow dr in dt.Rows) // But enumerating it works fine
{
// Some processing...
}
}
I have searched on the official docs of LINQ where I found
The standard query operators allow queries to be applied to any Enumerable-based information source.
I know that the reason should be something very simple, but I cannot figure out what is it. So here I go:
Why is LINQ for instances of System.Data.DataRowCollection
not allowed?
[EDIT]: There is already an answer for HOW to overcome this, but not to the WHY, so that's my purpose with this: to understand, not to overcome.