简体   繁体   中英

CopyToDataTable clears original metadata of DataTable

I am trying to sort a datatable. So, did the following code:

var dataTable = ds.Tables[DataTableName];
DataTable tempDataTable;
tempDataTable = dataTable.Clone();

tempDataTable = dataTable.AsEnumerable()                                                        
.OrderBy(x => x.Field<string>("fieldname"))                                                            
.ThenBy(x => x.Field<string>(sortColumn)).CopyToDataTable();

// issue here. it doesnt return the original table name but "Table1"
string tableN = tempDataTable.TableName; 

ds.Tables.Remove(dataTable);
ds.Tables.Add(tempDataTable); // add the sorted data table

Thanks

If you want to sort a DataTable you do not need to copy/clone it.

DataTable dataTable = Common.LoadFromDB();
dataTable.DefaultView.Sort = "fieldname ASC, " + sortColumn + " DESC";
dataTable.DefaultView.ToTable();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM