Fastest way to convert datatable to generic list

Joel Coehoorn picture Joel Coehoorn · Jan 13, 2009 · Viewed 31k times · Source

I have a data tier select method that returns a datatable. It's called from a business tier method that should then return a strongly typed generic List.

What I want to do is very similar (but not the same as) this question:
How do you convert a DataTable into a generic list?

What's different is that I want the list to contain strongly-typed objects rather than datarows (also, I don't have linq avaiable here yet).

I'm concerned about performance. The business tier method will in turn be called from the presentation tier, and the results will be iterated for display to the user. It seems very wasteful to add an extra iteration at the business tier, only do it again right away for the presentation, so I want this to be as quick as possible.

This is a common task, so I'm really looking for a good pattern that can be repeated over and over.

Answer

John picture John · May 21, 2010

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!