Linq to Select first few columns using index in datatable

Hey,

Using linq to select first 9 columns using their index in datatable1 and store the output in another datatable 2.

Any idea?

Hi @Ray_Sha1 ,

dtTest.AsEnumerable.Take(9).CopyToDataTable

dtTest = your data table

Thanks

1 Like

Hi @Ray_Sha1

How about this expression?

New DataTable → DtOutput

(From d In Dt.AsEnumerable
let ra = d.ItemArray.Take(9).toArray
Select DtOutput.Rows.Add(ra)).CopyToDataTable

Regards
Gokul

2 Likes

Hi @Ray_Sha1 ,

If you want to filter the Datatable to keep only the First 9 Columns, then you can Check the below Expression :

OutputDT = DT.DefaultView.ToTable(false,DT.Columns.Cast(Of DataColumn).Select(Function(x)x.ColumnName.ToString).Take(9).ToArray)
2 Likes

@Gokul001 @supermanPunch
The solution works.
Thanks!

1 Like

This takes in the whole first datatable

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.