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

      

  • 相关阅读:
    【RDB】MariaDB 之事务、复制、集群
    【中间件】Redis 实战之主从复制、高可用、分布式
    React从入门到放弃(5):ReactRouter4
    React从入门到放弃(4):Redux中间件
    React从入门到放弃(3):Redux简介
    React从入门到放弃(2):React简介
    React从入门到放弃(1):webpack4简介
    【.NET Core】ASP.NET Core之IdentityServer4(1):快速入门
    【.NET Core】Docker Jenkins ASP.NET Core自动化部署
    【ASP.NET Core】运行原理(4):授权
  • 原文地址:https://www.cnblogs.com/sovf/p/13441490.html
Copyright © 2011-2022 走看看