Add db.Utils.DataTableToDictionaryList

This commit is contained in:
skx 2021-01-25 20:44:11 +08:00
parent 13e8ebeab9
commit 069f2f8267
2 changed files with 24 additions and 0 deletions

View File

@ -572,6 +572,29 @@ namespace SqlSugar
return table.Rows.Cast<DataRow>().ToDictionary(x => x[0].ToString(), x => x[1]);
}
public List<Dictionary<string, object>> DataTableToDictionaryList(DataTable dt)
{
List<Dictionary<string, object>> result = new List<Dictionary<string, object>>();
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
for (int i = 0; i < dr.Table.Columns.Count; i++)
{
var value = dr[dr.Table.Columns[i].ColumnName];
if (value == DBNull.Value)
{
value = null;
}
dic.Add(dr.Table.Columns[i].ColumnName.ToString(), value);
}
result.Add(dic);
}
}
return result;
}
#endregion
#region Cache

View File

@ -29,6 +29,7 @@ namespace SqlSugar
List<T> DataTableToList<T>(DataTable table);
DataTable ListToDataTable<T>(List<T> list);
Dictionary<string, object> DataTableToDictionary(DataTable table);
List<Dictionary<string, object>> DataTableToDictionaryList(DataTable table);
ICacheService GetReflectionInoCacheInstance();
void RemoveCacheAll();
void RemoveCacheAll<T>();