IEnumerable 与DataTable互换

1.定义
在MSDN上,是这么说的,它是一个公开枚举数,该枚举数支持在非泛型集合上进行简单的迭代。换句话说,对于所有数组的遍历,都来自IEnumerable,那么我们就可以利用这个特性,来定义一个能够遍历数组的通用方法,这样看来,是不是很神奇呢?
例如:
        public static void Print(IEnumerable myList)
        {
            int i = 0;
            foreach (Object obj in myList)
            {
                if (obj is Student)//这个是类型的判断,这里Student是一个类或结构
                {
                    Student s=(Student)obj;
                    Console.WriteLine("\t[{0}]:\t{1}", i++, s.Sname);
                }
                if (obj is int)
                {
                    Console.WriteLine("INT:{0}",obj);
                }
            }
            Console.WriteLine();
        }
上面,我们可以在foreach中进行多个if判断,来进行相应的操作。
IEnumerable 的另一个用法是在泛型中的使用。其中用lamda表达式在数组中查询,具体例子如下:
            List<string> fruits =
                new List<string> { "apple", "passionfruit", "banana", "mango",
                    "orange", "blueberry", "grape", "strawberry" };
           // List<string> query = fruits.Where(fruit => fruit.Length < 6).ToList();
            IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6);
            foreach (string fruit in query)
                Console.WriteLine(fruit);
2.转换 IEnumerable 与DataTable互换
  1. /// <summary>  
  2.       /// DataTable 转换为List 集合  
  3.       /// </summary>  
  4.       /// <typeparam name="TResult">类型</typeparam>  
  5.       /// <param name="dt">DataTable</param>  
  6.       /// <returns></returns>  
  7.       public static List<TResult> ToList<TResult>(DataTable dt) where TResult : classnew()  
  8.       {  
  9.           //创建一个属性的列表  
  10.           List<PropertyInfo> prlist = new List<PropertyInfo>();  
  11.           //获取TResult的类型实例  反射的入口  
  12.           Type t = typeof(TResult);  
  13.           //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表   
  14.           Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });  
  15.           //创建返回的集合  
  16.           List<TResult> oblist = new List<TResult>();  
  17.   
  18.           foreach (DataRow row in dt.Rows)  
  19.           {  
  20.               //创建TResult的实例  
  21.               TResult ob = new TResult();  
  22.               //找到对应的数据  并赋值  
  23.               prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); });  
  24.               //放入到返回的集合中.  
  25.               oblist.Add(ob);  
  26.           }  
  27.           return oblist;  
  28.       }  
  29.   
  30.       /// <summary>  
  31.       /// 转换为一个DataTable  
  32.       /// </summary>  
  33.       /// <typeparam name="TResult"></typeparam>  
  34.       ///// <param name="value"></param>  
  35.       /// <returns></returns>  
  36.       public static DataTable ToDataTable(IEnumerable list)   
  37.       {  
  38.           //创建属性的集合  
  39.           List<PropertyInfo> pList = new List<PropertyInfo>();  
  40.           //获得反射的入口  
  41.           Type type = list.AsQueryable().ElementType;  
  42.           DataTable dt = new DataTable();  
  43.           //把所有的public属性加入到集合 并添加DataTable的列  
  44.           Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });  
  45.           foreach (var item in list)  
  46.           {  
  47.               //创建一个DataRow实例  
  48.               DataRow row = dt.NewRow();  
  49.               //给row 赋值  
  50.               pList.ForEach(p => row[p.Name] = p.GetValue(item, null));  
  51.               //加入到DataTable  
  52.               dt.Rows.Add(row);  
  53.           }  
  54.           return dt;  
  55.       }  
  56.   
  57.   
  58.       /// <summary>  
  59.       /// 转换为一个DataTable  
  60.       /// </summary>  
  61.       /// <typeparam name="TResult"></typeparam>  
  62.       ///// <param name="value"></param>  
  63.       /// <returns></returns>  
  64.       public static DataTable ToDataTable<TResult>(IEnumerable<TResult> value) where TResult : class  
  65.       {  
  66.           //创建属性的集合  
  67.           List<PropertyInfo> pList = new List<PropertyInfo>();  
  68.           //获得反射的入口  
  69.           Type type = typeof(TResult);  
  70.           DataTable dt = new DataTable();  
  71.           //把所有的public属性加入到集合 并添加DataTable的列  
  72.           Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });  
  73.           foreach (var item in value)  
  74.           {  
  75.               //创建一个DataRow实例  
  76.               DataRow row = dt.NewRow();  
  77.               //给row 赋值  
  78.               pList.ForEach(p => row[p.Name] = p.GetValue(item, null));  
  79.               //加入到DataTable  
  80.               dt.Rows.Add(row);  
  81.           }  
  82.           return dt;  
  83.       } 

猜你喜欢

转载自blog.csdn.net/typhoonwang/article/details/43125327