C#把list转化为Datatable

使用linq的时候,经常需要 select new{ ...不固定的字段... }  ,如何读取这些字段的单个值?通常两种做法:1)object对象反射是可以做的  2)在反射的基础上,转化为datatable 也可以

如下实例:

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using ConsoleApplication1;  
  7. using System.Data;  
  8. using System.Collections;  
  9. using System.Reflection;  
  10.   
  11. namespace ConsoleApplication1  
  12. {  
  13.     class Program  
  14.     {  
  15.         static void Main(string[] args)   
  16.         {  
  17.   
  18.             /*创建数据源*/  
  19.             List<myclass> l = new List<myclass>();  
  20.             for (int i = 0; i < 10; i++)  
  21.             {  
  22.                 myclass mc = new myclass();  
  23.                 mc.age = i;  
  24.                 mc.name = "myname" + i;  
  25.                 l.Add(mc);  
  26.             }  
  27.   
  28.             /*创建datatable*/  
  29.             DataTable dt = new DataTable();  
  30.   
  31.             /*返回一个有任意字段的 List<Object>对象,   select new 中的namefull就是自己随意添加的字段*/  
  32.             List<Object> rs = (from r in l select new { name = r.name, namefull = r.name + r.age, age = r.age }).ToList<Object>();  
  33.   
  34.             /*把List<Object>对象转化为Datatable对象*/  
  35.             dt = ToDataTableTow(rs);  
  36.   
  37.             /*读取Datatable对象*/  
  38.             foreach (DataRow dr in dt.Rows)  
  39.             {  
  40.                 /*按照拼接的字段名称 读取内容*/  
  41.                 Console.WriteLine(dr["name"].ToString() +"-----------------"+ dr["namefull"].ToString());  
  42.             }  
  43.             Console.Read();  
  44.         }  
  45.   
  46.   
  47.   
  48.         /// <summary>    
  49.         /// 将集合类转换成DataTable    
  50.         /// </summary>    
  51.         /// <param name="list">集合</param>    
  52.         /// <returns></returns>    
  53.         public static DataTable ToDataTableTow(IList list)  
  54.         {  
  55.             DataTable result = new DataTable();  
  56.             if (list.Count > 0)  
  57.             {  
  58.                 PropertyInfo[] propertys = list[0].GetType().GetProperties();  
  59.   
  60.                 foreach (PropertyInfo pi in propertys)  
  61.                 {  
  62.                     result.Columns.Add(pi.Name, pi.PropertyType);  
  63.                 }  
  64.                 for (int i = 0; i < list.Count; i++)  
  65.                 {  
  66.                     ArrayList tempList = new ArrayList();  
  67.                     foreach (PropertyInfo pi in propertys)  
  68.                     {  
  69.                         object obj = pi.GetValue(list[i], null);  
  70.                         tempList.Add(obj);  
  71.                     }  
  72.                     object[] array = tempList.ToArray();  
  73.                     result.LoadDataRow(array, true);  
  74.                 }  
  75.             }  
  76.             return result;  
  77.         }  
  78.     }  
  79.   
  80.     public class myclass  
  81.     {  
  82.   
  83.         public string name;  
  84.         public int age;  
  85.     }  
  86. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值