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