zoukankan      html  css  js  c++  java
  • C# DataTable转List<Model>通用类

    /// <summary>
    /// DataTable转换为List&lt;Model&gt;
    /// </summary>
    public static class DataTableToListModel<T> where T : new()
    {
        public static IList<T> ConvertToModel(DataTable dt)
        {
            //定义集合
            IList<T> ts = new List<T>();
            T t = new T();
            string tempName = "";
            //获取此模型的公共属性
            PropertyInfo[] propertys = t.GetType().GetProperties();
             foreach (DataRow row in dt.Rows)
            {         
                t = new T();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;
                    //检查DataTable是否包含此列
                    if (dt.Columns.Contains(tempName))
                    {
                        //判断此属性是否有set
                        if (!pi.CanWrite)
                            continue;
                        object value = row[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }
    }
  • 相关阅读:
    SecureCRT安装
    wmv12下安装centos7
    SpringMVC(十二):SpringMVC 处理输出模型数据之@ModelAttribute
    oracle之 redo过高诊断
    oracle之 手动创建 emp 表 与 dept 表
    oracle之 11.2.0.4 bbed安装
    oracle12c之 控制pdb中sga 与 pga 内存使用
    storm之 Storm 工作原理
    spark之 spark 2.2.0 Standalone安装、wordCount演示
    hadoop之 参数调优
  • 原文地址:https://www.cnblogs.com/sydeveloper/p/3044369.html
Copyright © 2011-2022 走看看