从 DataTable 填充类

发布于 2024-10-30 13:54:35 字数 633 浏览 1 评论 0原文

我有一个类需要从 DataTable 对象中进行水化。通常我以手动方式执行此操作。 (参见代码 snipit)。使用 ADO.NET 和 TSql 填充的 DataTable 对象。我需要将数据表中的值传输到我的 .NET 类中。有没有一种实用方法可以自动为我执行此操作?这样我就可以避免像下面这样的重复代码?

            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);

我知道实体框架是一个应该填补这一空白的工具。我还没有完全跳转到实体框架。现在我想要一个类似于 MVC 的实用方法 UpdateModel() 的方法,它是轻量级且简单的,并且通过将键名称与属性名称相匹配来从表单值对列表中生成一个类。

这样一个实用的方法会节省我大量的时间!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

氛圍 2024-11-06 13:54:35

就像上面提到的,我相信 AutoMapper 现在可以做到这一点。您还可以查看 ValueInjecterValueInjecter 和 DataTable

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文