我想在.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