你如何转换数据表为一个泛型列表? [英] How do you convert a DataTable into a generic list?
本文介绍了你如何转换数据表为一个泛型列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我使用的:
DataTable dt = CreateDataTableInSomeWay();
List<DataRow> list = new List<DataRow>();
foreach (DataRow dr in dt.Rows)
{
list.Add(dr);
}
有没有更好/魔法方法吗?
Is there a better/magic way?
推荐答案
如果您正在使用.NET 3.5中,你可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.datatableextensions.asenumerable.aspx\"><$c$c>DataTableExtensions.AsEnumerable$c$c> (扩展方法),然后如果你真的需要一个列表&LT; DataRow的&GT;
,而不是仅仅的IEnumerable&LT; DataRow的&GT;
你可以调用 Enumerable.ToList
:
If you're using .NET 3.5, you can use DataTableExtensions.AsEnumerable
(an extension method) and then if you really need a List<DataRow>
instead of just IEnumerable<DataRow>
you can call Enumerable.ToList
:
IEnumerable<DataRow> sequence = dt.AsEnumerable();
或
using System.Linq;
...
List<DataRow> list = dt.AsEnumerable().ToList();
这篇关于你如何转换数据表为一个泛型列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文