mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-24 18:04:52 +08:00
Add List to DataTable
This commit is contained in:
parent
85ca77fec7
commit
2fbd77ed74
@ -536,6 +536,37 @@ namespace SqlSugar
|
||||
}
|
||||
return this.DeserializeObject<List<T>>(this.SerializeObject(deserializeObject));
|
||||
}
|
||||
public DataTable ListToDataTable<T>(List<T> list)
|
||||
{
|
||||
DataTable result = new DataTable();
|
||||
if (list.Count > 0)
|
||||
{
|
||||
PropertyInfo[] propertys = list[0].GetType().GetProperties();
|
||||
foreach (PropertyInfo pi in propertys)
|
||||
{
|
||||
//获取类型
|
||||
Type colType = pi.PropertyType;
|
||||
//当类型为Nullable<>时
|
||||
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
|
||||
{
|
||||
colType = colType.GetGenericArguments()[0];
|
||||
}
|
||||
result.Columns.Add(pi.Name, colType);
|
||||
}
|
||||
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();
|
||||
result.LoadDataRow(array, true);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public Dictionary<string, object> DataTableToDictionary(DataTable table)
|
||||
{
|
||||
return table.Rows.Cast<DataRow>().ToDictionary(x => x[0].ToString(), x => x[1]);
|
||||
|
@ -27,6 +27,7 @@ namespace SqlSugar
|
||||
SqlSugarProvider CopyContext(bool isCopyEvents = false);
|
||||
dynamic DataTableToDynamic(DataTable table);
|
||||
List<T> DataTableToList<T>(DataTable table);
|
||||
DataTable ListToDataTable<T>(List<T> list);
|
||||
Dictionary<string, object> DataTableToDictionary(DataTable table);
|
||||
ICacheService GetReflectionInoCacheInstance();
|
||||
void RemoveCacheAll();
|
||||
|
Loading…
Reference in New Issue
Block a user