List<T>转换为DataTable

今天小姚碰到个问题,有个以前的程序直接调用数据库用dataset和datatable.但是现在想加一层Model层.获取List<T>后赋值给Table时碰到难题.

    本来我觉得这就是个简单的问题,将Table看做一张表,直接循环List后赋值即可.查了下资料才知道自己把DataTable想得太简单了....

     先说后来用到的方法一:

[csharp]  view plain  copy
 print ?
  1. List<info> infos = Dal.GetInfos();  
  2.         DataTable dt = new DataTable();  
  3.         dt.Columns.Add("cName");  
  4.   
  5.         foreach (var info in infos)  
  6.         {  
  7.             DataRow dr = dt.NewRow();  
  8.             dr["cName"] = info.Name;  
  9.             dt.Add(dr);  
  10.         }  


网上的:

 

[csharp]  view plain  copy
 print ?
  1. public static class DataTableExtensions  
  2. {  
  3.     /// <summary>    
  4.   
  5.     /// 转化一个DataTable    
  6.   
  7.     /// </summary>    
  8.   
  9.     /// <typeparam name="T"></typeparam>    
  10.     /// <param name="list"></param>    
  11.     /// <returns></returns>    
  12.     public static DataTable ToDataTable<T>(this IEnumerable<T> list)  
  13.     {  
  14.   
  15.         //创建属性的集合    
  16.         List<PropertyInfo> pList = new List<PropertyInfo>();  
  17.         //获得反射的入口    
  18.   
  19.         Type type = typeof(T);  
  20.         DataTable dt = new DataTable();  
  21.         //把所有的public属性加入到集合 并添加DataTable的列    
  22.         Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });  
  23.         foreach (var item in list)  
  24.         {  
  25.             //创建一个DataRow实例    
  26.             DataRow row = dt.NewRow();  
  27.             //给row 赋值    
  28.             pList.ForEach(p => row[p.Name] = p.GetValue(item, null));  
  29.             //加入到DataTable    
  30.             dt.Rows.Add(row);  
  31.         }  
  32.         return dt;  
  33.     }  
  34.   
  35.   
  36.     /// <summary>    
  37.     /// DataTable 转换为List 集合    
  38.     /// </summary>    
  39.     /// <typeparam name="TResult">类型</typeparam>    
  40.     /// <param name="dt">DataTable</param>    
  41.     /// <returns></returns>    
  42.     public static List<T> ToList<T>(this DataTable dt) where T : classnew()  
  43.     {  
  44.         //创建一个属性的列表    
  45.         List<PropertyInfo> prlist = new List<PropertyInfo>();  
  46.         //获取TResult的类型实例  反射的入口    
  47.   
  48.         Type t = typeof(T);  
  49.   
  50.         //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表     
  51.         Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });  
  52.   
  53.         //创建返回的集合    
  54.   
  55.         List<T> oblist = new List<T>();  
  56.   
  57.         foreach (DataRow row in dt.Rows)  
  58.         {  
  59.             //创建TResult的实例    
  60.             T ob = new T();  
  61.             //找到对应的数据  并赋值    
  62.             prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); });  
  63.             //放入到返回的集合中.    
  64.             oblist.Add(ob);  
  65.         }  
  66.         return oblist;  
  67.     }  
  68.   
  69.   
  70.   
  71.   
  72.     /// <summary>    
  73.     /// 将集合类转换成DataTable    
  74.     /// </summary>    
  75.     /// <param name="list">集合</param>    
  76.     /// <returns></returns>    
  77.     public static DataTable ToDataTableTow(IList list)  
  78.     {  
  79.         DataTable result = new DataTable();  
  80.         if (list.Count > 0)  
  81.         {  
  82.             PropertyInfo[] propertys = list[0].GetType().GetProperties();  
  83.   
  84.             foreach (PropertyInfo pi in propertys)  
  85.             {  
  86.                 result.Columns.Add(pi.Name, pi.PropertyType);  
  87.             }  
  88.             for (int i = 0; i < list.Count; i++)  
  89.             {  
  90.                 ArrayList tempList = new ArrayList();  
  91.                 foreach (PropertyInfo pi in propertys)  
  92.                 {  
  93.                     object obj = pi.GetValue(list[i], null);  
  94.                     tempList.Add(obj);  
  95.                 }  
  96.                 object[] array = tempList.ToArray();  
  97.                 result.LoadDataRow(array, true);  
  98.             }  
  99.         }  
  100.         return result;  
  101.     }  
  102.   
  103.   
  104.     /**/  
  105.   
  106.     /// <summary>    
  107.     /// 将泛型集合类转换成DataTable    
  108.   
  109.     /// </summary>    
  110.     /// <typeparam name="T">集合项类型</typeparam>    
  111.   
  112.     /// <param name="list">集合</param>    
  113.     /// <returns>数据集(表)</returns>    
  114.     public static DataTable ToDataTable<T>(IList<T> list)  
  115.     {  
  116.         return ToDataTable<T>(list, null);  
  117.   
  118.     }  
  119.   
  120.   
  121.     /**/  
  122.   
  123.     /// <summary>    
  124.     /// 将泛型集合类转换成DataTable    
  125.     /// </summary>    
  126.     /// <typeparam name="T">集合项类型</typeparam>    
  127.     /// <param name="list">集合</param>    
  128.     /// <param name="propertyName">需要返回的列的列名</param>    
  129.     /// <returns>数据集(表)</returns>    
  130.     public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)  
  131.     {  
  132.         List<string> propertyNameList = new List<string>();  
  133.         if (propertyName != null)  
  134.             propertyNameList.AddRange(propertyName);  
  135.         DataTable result = new DataTable();  
  136.         if (list.Count > 0)  
  137.         {  
  138.             PropertyInfo[] propertys = list[0].GetType().GetProperties();  
  139.             foreach (PropertyInfo pi in propertys)  
  140.             {  
  141.                 if (propertyNameList.Count == 0)  
  142.                 {  
  143.                     result.Columns.Add(pi.Name, pi.PropertyType);  
  144.                 }  
  145.                 else  
  146.                 {  
  147.                     if (propertyNameList.Contains(pi.Name))  
  148.                         result.Columns.Add(pi.Name, pi.PropertyType);  
  149.                 }  
  150.             }  
  151.   
  152.             for (int i = 0; i < list.Count; i++)  
  153.             {  
  154.                 ArrayList tempList = new ArrayList();  
  155.                 foreach (PropertyInfo pi in propertys)  
  156.                 {  
  157.                     if (propertyNameList.Count == 0)  
  158.                     {  
  159.                         object obj = pi.GetValue(list[i], null);  
  160.                         tempList.Add(obj);  
  161.                     }  
  162.                     else  
  163.                     {  
  164.                         if (propertyNameList.Contains(pi.Name))  
  165.                         {  
  166.                             object obj = pi.GetValue(list[i], null);  
  167.                             tempList.Add(obj);  
  168.                         }  
  169.                     }  
  170.                 }  
  171.                 object[] array = tempList.ToArray();  
  172.                 result.LoadDataRow(array, true);  
  173.             }  
  174.         }  
  175.         return result;  
  176.     }  
  177. }  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值