Aki999

I want to get all Elements of one DataColumn of a DataTable as a List or a IList without manually filling that list in a loop. How to do that


Re: Windows Forms Data Controls and Databinding How to get all Elements of one DataColumn

Ken Tucker

Try something like this.

Code Snippet

Dim ar(ds.Tables("Products").Rows.Count - 1) As DataRow
ds.Tables("Products").Rows.CopyTo(ar, 0)

Dim MyColumn(ds.Tables("Products").Rows.Count - 1) As String
MyColumn = Array.ConvertAll(ar, New Converter(Of DataRow, String)(AddressOf GetDataColumn))
End Sub

Public Shared Function GetDataColumn(ByVal dr As DataRow) As String

Return dr("ProductName").ToString
End Function

Note when Visual Studio 2008 is released later this year you will be able to get data like this with Linq

Code Snippet

Dim MyColumn = (From c In db.Products Select c.ProductName).ToList