/// <summary>
/// 集合装换DataTable
/// </summary>
/// <param name="list">集合</param>
/// <returns></returns>
public static DataTable ToDataTable(IList list)
{
 var dataTable = new DataTable();
 if (list.Count > 0)
 {
  PropertyInfo[] propertys = list[0].GetType().GetProperties();
  foreach (PropertyInfo pi in propertys)
  {
   dataTable.Columns.Add(pi.Name, pi.PropertyType);
  }
  for (int i = 0; i < list.Count; i++)
  {
   ArrayList tempList = new ArrayList();
   foreach (PropertyInfo pi in propertys)
   {
    object obj = pi.GetValue(list[i], null);
    tempList.Add(obj);
   }
   object[] array = tempList.ToArray();
   dataTable.LoadDataRow(array, true);
  }
 }
 return dataTable;
}

 

相关文章:

  • 2021-06-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-03
  • 2022-02-01
  • 2021-10-08
猜你喜欢
  • 2021-08-22
  • 2021-06-29
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2021-06-13
相关资源
相似解决方案