C#DataTable与List/Json/Dictionary互相转换
之前写过DataTable转List和List转DataTable的文章,今天在项目中偶然发现有Json转DataTable的公共方法,话不多说先写一篇博客备份一下,感觉以后要用到的地方还是蛮多的。
核心代码:
#region 数据表DataTable 转键值对集合 List
/// <summary>
/// 数据表DataTable 转键值对集合 List
/// 把DataTable转成 List集合, 存每一行
/// 集合中放的是键值对字典,存每一列
/// </summary>
/// <param name="dt">数据表</param>
/// <returns>哈希表数组</returns>
public static List<Dictionary<string, object>> DataTableToList(DataTable dt)
{
List<Dictionary<string, object>> list
= new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
foreach (DataColumn dc in dt.Columns)
{
dic.Add(dc.ColumnName, dr[dc.ColumnName]);
}
list.Add(dic);
}
return list;
}
#endregion
#region 数据集转键值对数组字典
/// <summary>
/// 数据集转键值对数组字典
/// </summary>
/// <param name="dataSet">数据集</param>
/// <returns>键值对数组字典</returns>
public static Dictionary<string, List<Dictionary<string, object>>> DataSetToDic(DataSet ds)
{
Dictionary<string, List<Dictionary<string, object>>> result = new Dictionary<string, List<Dictionary<string, object>>>();
foreach (DataTable dt in ds.Tables)
result.Add(dt.TableName, DataTableToList(dt));
return result;
}
#endregion
#region 数据表Tables 转JSON
/// <summary>
/// 数据表转Tables JSON
/// </summary>
/// <param name="dataTable">数据表</param>
/// <returns>JSON字符串</returns>
public static string DataTableToJSON(DataTable dt)
{
return JsonHelper.ObjectToJSON(DataTableToList(dt));
}
#endregion
#region 将datatable转换为json
/// <summary>
/// 将datatable转换为json
/// </summary>
/// <param name="dtb">Dt</param>
/// <returns>JSON字符串</returns>
public static string Dtb2Json(DataTable dtb)
{
//JavaScriptSerializer jss = new JavaScriptSerializer();
System.Collections.ArrayList dic = new System.Collections.ArrayList();
foreach (DataRow dr in dtb.Rows)
{
System.Collections.Generic.Dictionary<string, object> drow = new System.Collections.Generic.Dictionary<string, object>();
foreach (DataColumn dc in dtb.Columns)
{
drow.Add(dc.ColumnName, dr[dc.ColumnName]);
}
dic.Add(drow);
}
return null;
}
#endregion
#region 将Dictionary转换为数据表数据 Tables
public static DataTable DictToDataTable(Dictionary<string, object> dict)
{
DataTable dt = new DataTable();
//dt.Columns.Add("ID", typeof(Guid));
//dt.Columns.Add("REMARKS", typeof(string));
foreach (var colName in dict.Keys)
{
dt.Columns.Add(colName, typeof(string));
}
DataRow dr = dt.NewRow();
foreach (KeyValuePair<string, object> item in dict)
{
dr[item.Key] = item.Value;
}
dt.Rows.Add(dr);
return dt;
}
#endregion
#region 将List转换为数据表数据 Tables
/// <summary>
/// List转DataTable
/// </summary>
public static DataTable ListToDataTable<T>(List<T> list)
{
if (list == null || list.Count == 0)
{
return new DataTable();
}
//获取T下所有的属性
Type entityType = list[0].GetType();
PropertyInfo[] entityProperties = entityType.GetProperties();
DataTable dt = new DataTable("data");
for (int i = 0; i < entityProperties.Length; i++)
{
dt.Columns.Add(entityProperties[i].Name);
}
foreach (var item in list)
{
if (item.GetType() != entityType)
{
throw new Exception("要转换集合元素类型不一致!");
}
//创建一个用于放所有属性值的数组
object[] entityValues = new object[entityProperties.Length];
for (int i = 0; i < entityProperties.Length; i++)
{
entityValues[i] = entityProperties[i].GetValue(item, null);
}
dt.Rows.Add(entityValues);
}
return dt;
}
#endregion
#region Json 字符串 转换为 DataTable数据集合 简要版,正在使用中
/// <summary>
/// Json 字符串 转换为 DataTable数据集合 简要版,正在使用中
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
///
//格式;
//[{"mac":"20:f1:7c:c5:cd:80","rssi":"-86","ch":"9"},{"mac":"20:f1:7c:c5:cd:85","rssi":"-91","ch":"9"}]
public static DataTable ToDataTableTwo(string json)
{
DataTable dataTable = new DataTable(); //实例化
DataTable result;
try
{
List<Dictionary<string, object>> arrayList = JsonHelper.JSONToObject<List<Dictionary<string, object>>>(json);
if (arrayList != null && arrayList.Count > 0)
{
foreach (Dictionary<string, object> dictionary in arrayList)
{
if (dictionary.Keys.Count == 0)
{
result = dataTable;
return result;
}
//Columns
if (dataTable.Columns.Count == 0)
{
foreach (var current in dictionary)
{
if (current.Value != null)
{
dataTable.Columns.Add(current.Key, current.Value.GetType());
}
else
{
dataTable.Columns.Add(current.Key);
}
}
}
//Rows
DataRow dataRow = dataTable.NewRow();
foreach (string current in dictionary.Keys)
{
if (dictionary[current] != null)
{
dataRow[current] = dictionary[current];
}
}
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
}
}
}
catch (Exception ex)
{
throw ex;
}
result = dataTable;
return result;
}
#endregion
常见的基本都有了,如果不可用可以自己再二次开发一下,有些方法我也没怎么用过,不知道是不是还能使用,有问题可以在文章下方留言,看到我会第一时间回复。
版权声明:
作者:兴兴
文章:C#DataTable与List/Json/Dictionary互相转换
链接:https://www.networkcabin.com/notes/2894
文章版权归本站所有,未经授权请勿转载。
作者:兴兴
文章:C#DataTable与List/Json/Dictionary互相转换
链接:https://www.networkcabin.com/notes/2894
文章版权归本站所有,未经授权请勿转载。
THE END