【问题标题】:CopyToDataTable clears original metadata of DataTableCopyToDataTable 清除 DataTable 的原始元数据
【发布时间】:2018-10-12 05:24:59
【问题描述】:

我正在尝试对数据表进行排序。所以,做了以下代码:

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

谢谢

【问题讨论】:

  • 克隆可能不会复制表名。您仍然可以自己设置表名或使用 index 来代替
  • 谢谢。好的,有什么深层的 CopyToDataTable() 东西吗?
  • 我不确定,但如果您尝试ds.Tables[DataTableName].DefaultView.Sort =" FieldName SortDirection";会发生什么情况@

标签: c# asp.net linq datatable metadata


【解决方案1】:

如果您想对 DataTable 进行排序,则无需复制/克隆它。

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

【讨论】:

  • 这将在一列上排序。我需要两列..字段“fieldname”没有显示,但仅用于分组..
  • 它有 2 列。但是您不能在使用数据表的控件中显示fieldName 列。
猜你喜欢
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
  • 2017-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-09
相关资源
最近更新 更多