public static DataSet ToDataSet<TSource>(this IList<TSource> list)
{
Type elementType = typeof(TSource);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
ds.Tables.Add(dt);

foreach (var pi in elementType.GetProperties())
{
Type colType = Nullable.GetUnderlyingType(pi.PropertyType) ?? pi.PropertyType;
dt.Columns.Add(pi.Name, colType);
}

foreach (TSource item in list)
{
DataRow row = dt.NewRow();
foreach (var pi in elementType.GetProperties())
{
row[pi.Name] = pi.GetValue(item, null) ?? DBNull.Value;
}
dt.Rows.Add(row);
}

return ds;
}

相关文章:

  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
  • 2023-02-15
  • 2021-11-10
  • 2021-09-07
  • 2021-10-15
猜你喜欢
  • 2022-01-21
  • 2021-11-08
  • 2019-06-13
  • 2022-12-23
  • 2022-01-01
  • 2021-10-31
  • 2022-01-02
相关资源
相似解决方案