转换DataRowCollection到了IEnumerable&LT; T&GT; [英] Convert DataRowCollection to IEnumerable<T>
本文介绍了转换DataRowCollection到了IEnumerable&LT; T&GT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我愿做这样的事情在.NET 3.5。什么是最快的方法是什么?
的IEnumerable&LT; DataRow的&GT;集合=
TypedDataSet.TypedTableBase&LT; DataRow的&GT; .Rows为IEnumerable&LT; DataRow的取代;
解决方案
假设你正在使用.NET 4.0,它引入了协方差:
// presumably你的表是从TypedTableBase&LT某种类型的推导; T&GT ;,
//其中T是一个自动生成的类型从DataRow中派生。
IEnumerable的&LT; DataRow的&GT;集合= myTypedTable;
借助表类型本身工具的IEnumerable&LT; T&GT;其中,T:DataRow的
否则:
的IEnumerable&LT; DataRow的&GT;集合= myTypedTable.Cast&LT; DataRow的&GT;();
I would like to do something like this in .NET 3.5. What's the quickest way?
IEnumerable<DataRow> collection =
TypedDataSet.TypedTableBase<DataRow>.Rows as IEnumerable<DataRow>;
解决方案
Assuming you're using .NET 4.0, which introduces covariance:
// 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;
The table type itself implements IEnumerable<T> where T : DataRow
.
Otherwise:
IEnumerable<DataRow> collection = myTypedTable.Cast<DataRow>();
这篇关于转换DataRowCollection到了IEnumerable&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文