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

      

  • 相关阅读:
    时间复杂度计算(二)
    程序时间复杂度计算(一)
    一个图像算法岗的面试总结
    文本小票的一种无监督聚类方法
    多个C3P0的java举例
    基于投影和众数特点的粘连sku分割
    GLSL 中的光照计算
    openGL 提升渲染性能 之 顶点数组 VBO IBO VAO
    C++ 中的返回值
    游戏中逻辑线程和逻辑线程的并行
  • 原文地址:https://www.cnblogs.com/sovf/p/13441490.html
Copyright © 2011-2022 走看看