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

      

  • 相关阅读:
    约瑟夫问题
    十点半
    鹊桥相会
    C语言实验——数日子
    汉诺塔
    读入字符串
    C语言实验——各位数字之和排序
    数据结构实验之链表五:单链表的拆分
    C语言实验——分割整数
    大一上学期
  • 原文地址:https://www.cnblogs.com/sovf/p/13441490.html
Copyright © 2011-2022 走看看