• C# DataTable 转 List


    using System; 
    using System.Collections.Generic; 
    using System.Data; 
    using System.Reflection; 
    namespace jdrz.HumanIdentify { 
      public class Helper { /// <summary>         /// DataTable 转换为List 集合 /// </summary>         /// <typeparam name="TResult">类型</typeparam>         /// <param name="dt">DataTable</param>         /// <returns></returns> 
         public static List
    <TResult> ToList<TResult>(DataTable dt) where TResult : class, new()         { 
          //创建一个属性的列表 
          var prlist = new List<PropertyInfo>();

          //获取TResult的类型实例  反射的入口 
          var t = typeof(TResult); 

          //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表 
          Array.ForEach(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); }); 

          //创建返回的集合 
          var oblist = new List<TResult>(); 
          foreach (DataRow row in dt.Rows)
                { 
      
            //创建TResult的实例 
            
            var ob = new TResult(); 

    //找到对应的数据  并赋值 
    prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); }); 

    //放入到返回的集合中. oblist.Add(ob);             
    return oblist;        
     }     
    } }
  • 相关阅读:
    poj 1286 Necklace of Beads poj 2409 Let it Bead HDU 3923 Invoker <组合数学>
    大圣降妖录破解
    dex文件格式二
    dex文件格式一
    打造smali代码库辅助分析
    一键调试脚本使用手册
    TraceView进行性能分析
    Android Killer工具用法
    十三. JEB破解三
    十二. 一步步破解JEB 2.0demo版二
  • 原文地址:https://www.cnblogs.com/devgis/p/16524638.html
Copyright © 2020-2023  润新知