C#将集合类转换成DataTable

来源:互联网 发布:淘宝信誉等级 编辑:程序博客网 时间:2024/05/22 14:25
  1. // <summary>  
  2.         /// 将集合类转换成DataTable  
  3.         /// </summary>  
  4.         /// <param name="list">集合</param>  
  5.         /// <returns></returns>  
  6.         public static DataTable ToDataTable(IList list)  
  7.         {  
  8.             DataTable result = new DataTable();  
  9.             if (list.Count > 0)  
  10.             {  
  11.                 PropertyInfo[] propertys = list[0].GetType().GetProperties();  
  12.                 foreach (PropertyInfo pi in propertys)  
  13.                 {  
  14.                     result.Columns.Add(pi.Name, pi.PropertyType);  
  15.                 }  
  16.                 for (int i = 0; i < list.Count; i++)  
  17.                 {  
  18.                     ArrayList tempList = new ArrayList();  
  19.                     foreach (PropertyInfo pi in propertys)  
  20.                     {  
  21.                         object obj = pi.GetValue(list[i], null);  
  22.                         tempList.Add(obj);  
  23.                     }  
  24.                     object[] array = tempList.ToArray();  
  25.                     result.LoadDataRow(array, true);  
  26.                 }  
  27.             }  
  28.             return result;  
  29.         }  
  30.           
  31.         /// <summary>  
  32.         /// 将泛型集合类转换成DataTable  
  33.         /// </summary>  
  34.         /// <typeparam name="T">集合项类型</typeparam>  
  35.         /// <param name="list">集合</param>  
  36.         /// <returns>数据集(表)</returns>  
  37.         public static DataTable ToDataTable<T>(IList<T> list)  
  38.         {  
  39.             return ConvertX.ToDataTable<T>(list, null);  
  40.         }  
  41.          
  42.         /// <summary>  
  43.         /// 将泛型集合类转换成DataTable  
  44.         /// </summary>  
  45.         /// <typeparam name="T">集合项类型</typeparam>  
  46.         /// <param name="list">集合</param>  
  47.         /// <param name="propertyName">需要返回的列的列名</param>  
  48.         /// <returns>数据集(表)</returns>  
  49.         public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)  
  50.         {  
  51.             List<string> propertyNameList = new List<string>();  
  52.             if (propertyName != null)  
  53.                 propertyNameList.AddRange(propertyName);  
  54.             DataTable result = new DataTable();  
  55.             if (list.Count > 0)  
  56.             {  
  57.                 PropertyInfo[] propertys = list[0].GetType().GetProperties();  
  58.                 foreach (PropertyInfo pi in propertys)  
  59.                 {  
  60.                     if (propertyNameList.Count == 0)  
  61.                     {  
  62.                         result.Columns.Add(pi.Name, pi.PropertyType);  
  63.                     }  
  64.                     else  
  65.                     {  
  66.                         if (propertyNameList.Contains(pi.Name))  
  67.                             result.Columns.Add(pi.Name, pi.PropertyType);  
  68.                     }  
  69.                 }  
  70.                 for (int i = 0; i < list.Count; i++)  
  71.                 {  
  72.                     ArrayList tempList = new ArrayList();  
  73.                     foreach (PropertyInfo pi in propertys)  
  74.                     {  
  75.                         if (propertyNameList.Count == 0)  
  76.                         {  
  77.                             object obj = pi.GetValue(list[i], null);  
  78.                             tempList.Add(obj);  
  79.                         }  
  80.                         else  
  81.                         {  
  82.                             if (propertyNameList.Contains(pi.Name))  
  83.                             {  
  84.                                 object obj = pi.GetValue(list[i], null);  
  85.                                 tempList.Add(obj);  
  86.                             }  
  87.                         }  
  88.                     }  
  89.                     object[] array = tempList.ToArray();  
  90.                     result.LoadDataRow(array, true);  
  91.                 }  
  92.             }  
  93.             return result;  
  94.         }  
原创粉丝点击