如何将特定行从DataTable复制到c#中的另一个Datable?会有不止一行.
foreach (DataRow dr in dataTable1.Rows) { if (/* some condition */) dataTable2.Rows.Add(dr.ItemArray); }
上面的示例假设dataTable1
并且dataTable2
具有相同的数量,类型和列顺序.
将指定的行从表复制到另一个
// here dttablenew is a new Table and dttableOld is table Which having the data dttableNew = dttableOld.Clone(); foreach (DataRow drtableOld in dttableOld.Rows) { if (/*put some Condition */) { dtTableNew.ImportRow(drtableOld); } }
试试这个
String matchString="ID0001"//assuming we have to find rows having key=ID0001 DataTable dtTarget = new DataTable(); dtTarget = dtSource.Clone(); DataRow[] rowsToCopy; rowsToCopy = dtSource.Select("key='" + matchString + "'"); foreach (DataRow temp in rowsToCopy) { dtTarget.ImportRow(temp); }
看看这个,你可能会喜欢它(以前请将table1克隆到table2):
table1.AsEnumerable().Take(recodCount).CopyToDataTable(table2,LoadOption.OverwriteChanges);
要么:
table1.AsEnumerable().Where ( yourcondition ) .CopyToDataTable(table2,LoadOption.OverwriteChanges);
受支持:4,3.5 SP1,您现在可以只调用该对象上的方法.
DataTable dataTable2 = dataTable1.Copy()
由于其他职位,这是我能得到的最短的时间:
DataTable destTable = sourceTable.Clone(); sourceTable.AsEnumerable().Where(row => /* condition */ ).ToList().ForEach(row => destTable.ImportRow(row));