zoukankan      html  css  js  c++  java
  • ModelConvertHelper(将DataTable转换成List<model>)

    public class ModelConvertHelper<T> where T : new()
    {
         public static IList<T> ConvertToModel(DataTable dt)
         {
             // Collection definition
             IList<T> ts = new List<T>();

             // Get model type
             Type type = typeof(T);

             string tempName = "";

             foreach (DataRow dr in dt.Rows)
             {
                 T t = new T();

                 // Get property of model
                 PropertyInfo[] propertys = t.GetType().GetProperties();

                 foreach (PropertyInfo pi in propertys)
                 {
                     tempName = pi.Name;

                     // check column is exsit
                     if (dt.Columns.Contains(tempName))
                     {
                         // Whether can be set value
                         if (!pi.CanWrite) continue;

                         object value = dr[tempName];
                         if (value != DBNull.Value)
                             pi.SetValue(t, value, null);
                     }
                 }

                 ts.Add(t);
             }

             return ts;
         }
    }

  • 相关阅读:
    win10-wifi无线共享自动关闭解决
    可用的nlog配置
    cmake 常用指令,变量
    window时间服务
    命令行配置服务启动类型
    boost流gzip压缩
    mysql 查询某表的所有列,获取毫秒时间戳
    system进程占用80端口
    centos8重新分区(减小/home空间,增大root空间)
    emqx使用data_to_webservice方式配置规则引擎简单实践
  • 原文地址:https://www.cnblogs.com/key1309/p/3566567.html
Copyright © 2011-2022 走看看