zoukankan      html  css  js  c++  java
  • C#匿名转换 数据表转换

    调用方式:
    using System.Collections.Generic;
    List<Model> list = Common.Uitil.TableToEntity<Model>(ds.Tables[0]);
    return list[0];  
    方法:
    using System.Reflection;
    using System.Data;
    /// <summary>
            ///     
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static List<T> TableToEntity<T>( DataTable dt) where T : class,new()
            {
                Type type = typeof (T);
                List<T> list = new List<T>();
                foreach (DataRow row in dt.Rows)
                {
                    PropertyInfo[] pArray = type.GetProperties();
                    T entity = new T();
                    foreach (PropertyInfo p in pArray)
                    {
                        try
                        {
                            if (row[p.Name] is Int64)
                            {
                                p.SetValue(entity, Convert.ToInt32(row[p.Name]), null );
                                continue;
                            }
                            p.SetValue(entity, row[p.Name], null);
                        }
                        catch (Exception e) { //continue;
                        }
                    }
                    list.Add(entity);
                }
               
                return list;
            }
    

      

  • 相关阅读:
    【历史时刻】从学生到社会独立人——硕士毕业
    Linux 常用命令
    LInux系统下搭建redis集群
    docker 下创建自定义网络,并在运行容器时绑定网络和ip
    docker下安装mysql镜像
    windows下将consul注册为系统服务
    Sql批量替换字段字符,Sql批量替换多字段字符,Sql替换字符
    gerrit安装配置
    Linux安装jdk8及环境变量配置
    iTerm2配置优化
  • 原文地址:https://www.cnblogs.com/sovf/p/13441490.html
Copyright © 2011-2022 走看看