c#list与datatable 用哪个

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#list与datatable 用哪个相关的知识,希望对你有一定的参考价值。

如果处理简单的列表数据,建议用list存储数据;如果是比较复杂的数据,建议用table处理 参考技术A DataTable 内存中的数据表 一个二维表
List<model>泛型数组 这里面存放的是一个一个的被对象化的数据模型

C#DataTable(转List /JSON/字典 互转)

    #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("DID", typeof(string));
             //dt.Columns.Add("DEPARTMENTNUM", typeof(string));
             //dt.Columns.Add("DEPARTMENTNAME", typeof(string));
             //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#list与datatable 用哪个的主要内容,如果未能解决你的问题,请参考以下文章

怎么获取DataTable中某一字段的值

MySqlBulkLoader 使用 DataTable 而不是文件

我怎么知道列名?

C#DataTable(转List /JSON/字典 互转)

SqlDataReader 与 SqlDataAdapter:返回 DataTable 的性能哪个更好?

数据结构之线性表