|
|
A user recently asked me a question on converting a DataTable to a List<> in .NET 2.0. Here’s the code to do so:
C#
// Assuming there is a DataTable called dt
List<DataRow> drlist = new List<DataRow>();
foreach (DataRow row in dt.Rows)
{
drlist.Add((DataRow)row);
}
VB.NET
' Assuming there is a DataTable called dt
Dim drlist As New List(Of DataRow)()
For Each row As DataRow In dt.Rows
drlist.Add(CType(row, DataRow))
Next 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();
'Like' us on our FaceBook page if you find this blog useful. Thanks!
Did you like this post?
|
|
|
||
|
|
|
|
Save on Delicious |
|
|
subscribe via rss |
|
subscribe via e-mail |
|
|
print this post |
|
follow me on twitter |





comments
2 Responses to "Convert a DataTable to a List<> using C# or VB.NET"thanks for the post it was very useful
protected void RetrieveData_Click(object sender, EventArgs e)
{
List drList = new List();
foreach (DataRow row in dt.Rows)
{
drList.Add((DataRow)row);
}
foreach (DataRow s in drList)
{
Response.Write(s.ItemArray[0].ToString()+" "+s.ItemArray[1].ToString()+"
");
}
}
List list = new List(dataTable.Select());
List customList = list.ConvertAll(e=>new CustomType{ Member1= (e["Processed"])
});
Post a Comment