如何把EntityList转换成DataSet

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;
}

posted on 2018-12-06 13:37  AbelAngelo  阅读(366)  评论(0编辑  收藏  举报

导航