我想在.NET 3.5中做这样的事情。什么是最快的方法?将DataRowCollection转换为IEnumerable <T>
IEnumerable<DataRow> collection =
TypedDataSet.TypedTableBase<DataRow>.Rows as IEnumerable<DataRow>;
我想在.NET 3.5中做这样的事情。什么是最快的方法?将DataRowCollection转换为IEnumerable <T>
IEnumerable<DataRow> collection =
TypedDataSet.TypedTableBase<DataRow>.Rows as IEnumerable<DataRow>;
假设你正在使用.NET 4.0,它引入了协方差:
// Presumably your table is of some type deriving from TypedTableBase<T>,
// where T is an auto-generated type deriving from DataRow.
IEnumerable<DataRow> collection = myTypedTable;
的table type itself工具IEnumerable<T> where T : DataRow
。
否则:
IEnumerable<DataRow> collection = myTypedTable.Cast<DataRow>();
你可以叫上DataRowCollection
OfType<DataRow>()
。
一种简单直接的解决方案是使用该方法 “选择()” 一个System.Data.DataTable对象,其产生 “的DataRow []” 的。从这个可以作为一个IEnumberable使用LINQ像下面对待:
List<MyItem> items = dtItems.Select().Select(row => new MyItem(row)).ToList();
为每个行对象的有用列表。
如果您的System.Data.DataSetExtensions.dll
包含在您的项目中,并且添加了AsEnumerable()
方法,则有内置扩展方法。
IEnumerable<DataRow> collection = TypedDataSet.TypedTableBase<DataRow>.AsEnumerable();
这是最简单的解决方案 – 2015-05-29 20:48:51
应该是公认的答案! – 2016-08-24 09:25:03