仅将特定列的数据从Datatable1复制到Datattable2。我有一个方法,列vlaues的列表和Datatable作为输入。我需要从输入数据表中获取所有列,然后复制到新的数据表中。
/*Input:
columnNames in list : column1,column2,column3
datatable1 : column1,column2,column5,column6,column3
Output:
datatble2 : column1,column2,column3 (columns from list need to be copied from datatble1 to datatble2 and return datatble2)
*/
public DataTable CopyFromDatatbale(List<string> columnNames,DataTable datatable1)
{
DataTable datatble2=new DataTable();
/*
Code to copy the data from datatable1 to datatble2 with specific columns
*/
}
答案 0 :(得分:0)
这是我正在寻找的代码...将指定列的数据从DataTable1复制到DataTable2。最后在datatable2中,我具有datatble1的所有列(仅特定列)
//Copy Columns from Datatable1 to Datatble2 based on columns on columnList
DataView dtView = new DataView(dataTable1);
DataTable dataTable2= new DataTable();
var getColumnNamesCommaSeperated = columnList.Select(x => x.columnNames).ToArray();
dataTable2= dataTable1.Select().CopyToDataTable()
.DefaultView.ToTable(false, getColumnNamesCommaSeperated);