简体   繁体   中英

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();

However, what if wanted to convert to a list of some entity type? For instance, I have an entity class 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. What I want to do is convert the DataSet (or a DataTable) into a List

DataSet ds = ("call SP"); List = ds.something;

You can consider LINQ in this case.

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. 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:

How to convert dataset or dbreader to List(of T) or BindingList(Of T) using mapping

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

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

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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