如何在 Asp.Net MVC 中将数据表转换为 POCO 对象? [英] How do I convert a datatable into a POCO object in Asp.Net MVC?

查看:36
本文介绍了如何在 Asp.Net MVC 中将数据表转换为 POCO 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Asp.Net MVC 中将数据表转换为 POCO 对象?

How do I convert a datatable into a POCO object in Asp.Net MVC?

推荐答案

将每个 DataRow 传递给类构造函数(或使用 getter/setter)并将每一列转换为相应的属性.小心处理可为空的列以正确提取它们.

Pass each DataRow into the class constructor (or use getters/setters) and translate each column into the corresponding property. Be careful with nullable columns to extract them properly.

  public class POCO
  {
       public int ID { get; set; }
       public string Name { get; set; }
       public DateTime? Modified { get; set; }
       ...

       public POCO() { }

       public POCO( DataRow row )
       {
            this.ID = (int)row["id"];
            this.Name = (string)row["name"];
            if (!(row["modified"] is DBNull))
            {
                 this.Modified = (DateTime)row["modified"];
            }
            ...
       }
  }

这篇关于如何在 Asp.Net MVC 中将数据表转换为 POCO 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆