将数据集数据表转换为 IList<DataTable> 类型



我有一个包含多个数据表的数据集。现在我想复制这些数据表到IList对象。

var tables = new[] { DT1, DT2 };   //I want to change this line of code to pull the datatables from the dataset.
bool test = Funx(tables);
private bool Funx(IList<DataTable> tbls)
{
   ///some operation..
}

但是在我的例子中,数据集可以包含任意数量的数据表。我如何准备表var对象与所有数据表从数据集。

请建议。

您可以使用Cast + ToList:

IList<DataTable> tables = dataSet.Tables.Cast<DataTable>().ToList();

您需要使用Enumerable.Cast,因为DataTableCollection(由Tables返回)实现IEnumerable而不是IEnumerable<DataTable>(类是旧的):

最新更新