Convert a DataTable to a List<> using C#

Feb 10, 2010 Posted by Lara Kannan
Question is...
Convert a DataTable to a List<> using C#

To convert a DataTable to a List<> in .NET 2.0.

Here’s the code to do so:


// Assuming there is a DataTable called dt
List<DataRow> drlist = new List<DataRow>();

foreach (DataRow row in dt.Rows)
{
drlist.Add((DataRow)row);
}

Please note that this is just a prototype and may not cover all scenarios.

Note: I have not tested this but in .NET 3.5, you should be able to do this:

List<DataRow> drlist = dt.AsEnumerable().ToList();


Share
Labels: ,

Post a Comment