C# List转DataSet

来源:互联网 发布:mac草莓红和番茄红 编辑:程序博客网 时间:2024/05/19 10:33
 static class Extensions    {        internal static DataSet ToDataSet<T>(this List<T> list)        {            Type elementType = typeof(T);            var ds = new DataSet();            var t = new DataTable();            ds.Tables.Add(t);            elementType.GetProperties().ToList().ForEach(propInfo => t.Columns.Add(propInfo.Name, Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType));            foreach (T item in list)            {                var row = t.NewRow();                elementType.GetProperties().ToList().ForEach(propInfo => row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value);                t.Rows.Add(row);            }            return ds;        }    }  
0 0
原创粉丝点击