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

      

  • 相关阅读:
    Vpython简单例子
    我在读的书:《ACM图灵奖:19662006(第三版)计算机发展史的缩影》
    可惜啊,没见到姚期智~~
    The Sounds of Music 观后感
    终于在博客园申请开通博客了
    【引用】Python open读写文件实现脚本
    在Python中使用中文
    Discovery:深入理解计算机系统 (Ver.2) 中文版
    [导入]一个都不能少:全面认识IE插件
    [导入]午间心情
  • 原文地址:https://www.cnblogs.com/sovf/p/13441490.html
Copyright © 2011-2022 走看看