DataSet.Clone 会拷贝表结构,关联关系也会拷贝, 用Select 筛选后ImportRow 导入新的DataTable,然后处理关联DataTable
DataSet ds2 = dsSource.Clone(); if (condition != "1 > 2") { foreach (DataRow dr in dsSource.Tables[0].Select(condition)) { ds2.Tables[0].ImportRow(dr); //关联表数据处理 foreach (DataRelation relation in dsSource.Tables[0].ChildRelations) { string relationTableName = relation.ChildTable.TableName; DataRow[] relationRows = dr.GetChildRows(relation); foreach (DataRow childRow in relationRows) { ds2.Tables[relationTableName].ImportRow(childRow); } } } } return ds2;