Convert DataRowCollection to IEnumerable<T>
Asked Answered
S

4

78

I would like to do something like this in .NET 3.5. What's the quickest way?

IEnumerable<DataRow> collection = 
    TypedDataSet.TypedTableBase<DataRow>.Rows as IEnumerable<DataRow>;
Safelight answered 11/2, 2011 at 21:18 Comment(0)
J
85

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>();
Jurdi answered 11/2, 2011 at 21:20 Comment(1)
This and wsanville's answers were relevant for me. It happens that Cast<TResult> throws an exception when it finds an item which cannot be cast to TResult. On the other hand, OfType<DataRow> only returns items which are of type TResult. See the documentation on msdn.microsoft.com/en-us/library/bb360913(v=vs.110).aspxTogo
C
102

You can call OfType<DataRow>() on the DataRowCollection.

Cower answered 11/2, 2011 at 21:20 Comment(4)
This is the simplest solutionPoohpooh
This is really no different than calling Cast<DataRow>() on the DataRowCollection, which is already in the accepted answer. (except if we're splitting hairs, I believe OfType does some unnecessary filtering that Cast doesn't bother with)Arcboutant
@KevinHolt no I would say they are the same if and only if you know for certain the only type of objects in the collection are castable to DataRows... in the case of a DataRowCollection, that's true, but in most non-generic IEnumerables it definitely isn't.Vining
Right I'm not saying that IEnumerable<T>.OfType<R>() and IEnumerable<T>.Cast<R>() always do the same thing for any T and R, and I wasn't trying to imply that. However, I am saying (and it sounds like you agree with me) that they do the same thing in the case where R=T, such as the OP's actual question which I read as asking specifically about T=R=DataRow.Arcboutant
J
85

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>();
Jurdi answered 11/2, 2011 at 21:20 Comment(1)
This and wsanville's answers were relevant for me. It happens that Cast<TResult> throws an exception when it finds an item which cannot be cast to TResult. On the other hand, OfType<DataRow> only returns items which are of type TResult. See the documentation on msdn.microsoft.com/en-us/library/bb360913(v=vs.110).aspxTogo
H
5

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 IEnumerable<DataRow> using Linq like below:

List<MyItem> items = dtItems.Select()
                            .Select(row => new MyItem(row))
                            .ToList();

Providing a useful list of objects for each row.

Helpful answered 9/1, 2017 at 1:42 Comment(1)
The problem using .Select() is that if the data is ordered in the Datatable, it will lose it.Settlement
S
1

There is a built in extension method if you include System.Data.DataSetExtensions.dll in to your project that adds a AsEnumerable() method.

IEnumerable<DataRow> collection = TypedDataSet.TypedTableBase<DataRow>.AsEnumerable();
Screen answered 10/10, 2017 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.