简体   繁体   English

如何将DataSet / DataTable转换为某种类型的通用列表?

[英]How do you convert a DataSet/DataTable into a generic list of certain type?

I have looked at the answered topic with the answer: List list = dt.AsEnumerable().ToList(); 我已经用答案看了回答的主题 :列表列表= dt.AsEnumerable()。ToList();

However, what if wanted to convert to a list of some entity type? 但是,如果要转换为某些实体类型的列表怎么办? For instance, I have an entity class Property: 例如,我有一个实体类Property:

public class Property
    {
        public int ID { get; set; }
        public string Chain { get; set; }
        public string Name { get; set; }
        public string Street { get; set; }
        public string Street2 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }
        public string Phone { get; set; }
        public string FAX { get; set; }
        public string Longitude { get; set; }
        public string Latitude { get; set; }
        public string Airport { get; set; }
    }

I have to work with an Oracle SP that returns a DataSet. 我必须使用返回数据集的Oracle SP。 What I want to do is convert the DataSet (or a DataTable) into a List 我要做的是将数据集(或数据表)转换为列表

DataSet ds = ("call SP"); DataSet ds =(“ call SP”); List = ds.something; 列表= ds.something;

You can consider LINQ in this case. 在这种情况下,您可以考虑使用LINQ。

var list = (from dr in dt.AsEnumerable()
           select new Property { .... }).ToList();

Creates the List as result. 创建结果列表。

Edit : Made the first sentence mild. 编辑:使第一句话温和。

Just a side note. 只是一个旁注。 I've gone down this path before and have found that most of the time I didn't need some sort of entity class wrapper around the datatable. 我以前走这条路,发现大多数时候我不需要围绕数据表的某种实体类包装器。 Instead, the normal rows/columns access of the datatable itself was just fine. 相反,对数据表本身的常规行/列访问就很好。 It saved processing time and memory consumption by just passing the table around. 仅通过传递表就可以节省处理时间和内存消耗。

Typically speaking if I'm pulling the data from a proc for display, I leave it in a table. 通常来说,如果我要从proc中提取数据进行显示,则会将其保留在表格中。 However, if I have a class which includes additional functionality beyond just holding data, then I might convert the datatable into a list/collection of objects. 但是,如果我有一个类,它不仅包含保存数据,还包括其他功能,那么我可能会将数据表转换为对象的列表/集合。

On the flip side if data is coming back from the browser, then I'll populate an object whose class definition has all of the associated validation tied in prior to storing in the database. 另一方面,如果数据是从浏览器返回的,那么我将填充一个对象,该对象的类定义在存储到数据库中之前已绑定了所有相关的验证。

The point is, unless the entity class is going to give you something you don't already have then I wouldn't bother. 关键是,除非实体类为您提供您还没有的东西,否则我不会打扰。

Although this is in VB, you may be able to convert it to C# and use it: 尽管这在VB中,但是您可以将其转换为C#并使用它:

How to convert dataset or dbreader to List(of T) or BindingList(Of T) using mapping 如何使用映射将数据集或dbreader转换为List(of T)或BindingList(Of T)

http://prglog.blogspot.com/2009/10/how-to-convert-listof-t-or.html http://prglog.blogspot.com/2009/10/how-to-convert-listof-t-or.html

It looks like it uses reflection and will work with any type. 看起来它使用反射,并且可以使用任何类型。

You could also use the LINQ Select() method 您还可以使用LINQ Select()方法

DataTable dt = new DataTable
var list = dt.Rows.Select(row => new Propert
{
SomeProperty = row["columnName"],//etc...
}).ToList();

Another example: 另一个例子:

I wrote this article a few months ago: 我几个月前写了这篇文章:

http://www.codeproject.com/KB/linq/FromDataTable2GenericList.aspx http://www.codeproject.com/KB/linq/FromDataTable2GenericList.aspx

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM