Fastest way to convert datatable to generic list
The problem with the sample above is that it is terribly slow. I have a DataTable with about 400 rows and this conversion takes a good 5 or 6 seconds!
It does seem like a pretty common task, so I'm surprised to not see someone here with a more performant solution.
* Update!! * Just for kicks, I thought I would try converting using LINQ instead of iterating through the DataTable and adding to my List. The following is what I did:
List<MyObject> returnList = new List<MyObject>();
MyDataTable dtMyData = new MyTableAdapter().GetMyData();
returnLists = (from l in dtMyData
select new MyObject
{
Active = l.IsActive,
Email = l.Email,
//...
//About 40 more properties
//...
ZipCode = l.Zipcode
}).ToList();
The first method (iterating through each row) took 5.3 seconds and the method using LINQ took 1.8 seconds!
Do you know the structure of the DataTable
and the typed object ahead of time? You could use a delegate to do the mapping. If you don't (i.e. all you know is a Type
and properties) there are ways of accelerating dynamic member access (such as HyperDescriptor
).
Either way, consider an iterator block; that way you don't have to buffer the objects an entire second time; of course, if you are only dealing with smallish rowcounts this isn't an issue.
Can you clarify any of those points? I can add a lot more detail...
At the simplest, what is wrong with:
DataTable table = new DataTable {
Columns = {
{"Foo", typeof(int)},
{"Bar", typeof(string)}
}
};
for (int i = 0; i < 5000; i++) {
table.Rows.Add(i, "Row " + i);
}
List<MyType> data = new List<MyType>(table.Rows.Count);
foreach (DataRow row in table.Rows) {
data.Add(new MyType((int)row[0], (string)row[1]));
}
(the problems in the above might steer the right approach...)