开发者

Populate class from DataTable

开发者 https://www.devze.com 2023-02-21 16:01 出处:网络
I have a class that I need to hydrate from a DataTable object. Usually I do this the manual way. (see code snipit). The DataTable object populated using ADO.NET and TSql. I need to transfer the values

I have a class that I need to hydrate from a DataTable object. Usually I do this the manual way. (see code snipit). The DataTable object populated using ADO.NET and TSql. I need to transfer the values from the DataTable into my .NET class. Is there a utility method that will do this for me automagically? So that I can avoid repetitive code like the following?

            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 is supposed to fill this gap. I haven't quite made the jump to Entity Framework. For now I'd like to have a method that is similar to MVC's utility method UpdateModel() Which is lightweight and simple and hydrates a class from a list of form-value pairs by matching up key names with property names.

Such a utility method would save me tons of time!


Like mentioned above I believe AutoMapper can do this now. You could also look at a ValueInjecter. ValueInjecter and DataTable

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号