Convert DataRowCollection to IEnumerable<T>
Solution 1:
You can call OfType<DataRow>()
on the DataRowCollection
.
Solution 2:
Assuming you're using .NET 4.0, which introduces covariance:
// Presumably your table is of some type deriving from TypedTableBase<T>,
// where T is an auto-generated type deriving from DataRow.
IEnumerable<DataRow> collection = myTypedTable;
The table type itself implements IEnumerable<T> where T : DataRow
.
Otherwise:
IEnumerable<DataRow> collection = myTypedTable.Cast<DataRow>();
Solution 3:
A simple direct solution is to use the method "Select()" of a System.Data.DataTable object, which produces "DataRow[]". From this you can treat as an IEnumberable using Linq like below:
List<MyItem> items = dtItems.Select().Select(row => new MyItem(row)).ToList();
Providing a useful list of objects for each row.