Fill a class from a DataTable

I have a class that I need to moisten from a DataTable. I usually do it manually. (see snipit code). The DataTable is populated using ADO.NET and TSql. I need to transfer values ​​from a DataTable to a .NET class. Is there a utility method that will do this for me automatically? So that I can avoid duplicate code, for example:

            DriverSummary driver = new DriverSummary();
            driver.Id = (int)row["Id"];
            driver.UserId = row["UserId"] as string;
            driver.Name = row["Name"] as string;
            driver.TruckType = row["TruckType"] as string;
            summaries.Add(driver);

I know that the Entity Framework is a tool that should fill this gap. I did not quite make the transition to the Entity Framework. For now, I would like to have a method similar to the MVC utility method. UpdateModel (), which is light and simple, moisturizes a class from a list of pairs of form values ​​by matching key names with property names.

Such a useful method will save me a lot of time!

+5
source share
1 answer

As mentioned above, I believe AutoMapper can do this now. You can also look at ValueInjecter . ValueInjecter and DataTable

+4
source

All Articles