C#数据表(DataTable)转键值对集合


/// <summary> /// 数据表转键值对集合 /// 把DataTable转成 List集合, 存每一行 /// 集合中放的是键值对字典,存每一列 ,键/值相同 /// </summary> /// <param name="dt">数据表</param> /// <returns>哈希表数组</returns> public static List<Dictionary<string, object>> DataToList(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; }

 

 转换成

 

posted @ 2019-10-16 20:53  袁浩178  阅读(1536)  评论(1编辑  收藏  举报