I have the following LINQ query which will group by a column and add each group to a datatable in a dataset:
Dim ds As New DataSet
Dim query = From r In bookedorders Group By key = r.Field(Of Integer)("productgroup") Into Group
For Each grp In query
Dim x As New DataTable
x = grp.Group.CopyToDataTable()
ds.Tables.Add(x)
Next
How can I select specific columns from table bookedorders in this query before adding them to the DataTable?
I have 18 columns and I just want to show 4 in the resulted DataTable.
You can only create a DataTable via
CopyToDataTablewith anIEnumerable(Of DataRow). If you would select only few columns via an anonymous type, you couldn’t create aDataTablefrom it anymore.So you need to create another DataTable first with the columns you want to select.
For example: