C#中list转table以及table转list

哈哈哈,同志们,相信我,绝对好用

using System.Data.SqlClient;
using System.Reflection;
using System.Collections.Generic;


//datatable转list
DataTable data= DBHelper.FillDataTable(sql, null);
             List<TableFieldDto> list = new List<TableFieldDto>();
            
             list = (from DataRow dr in data.Rows
                     select new TableFieldDto
                     {
                           
                         GridViewField = dr["GridViewField"].ToString(),
                         FieldDesc = dr["FieldDesc"].ToString(),                      
                     }).ToList();
            
             return list;    
//list转datatable
   DataTable dt = new DataTable();                         
               List<Calculation_inventoryDto> selectdata = JsonConvert.DeserializeObject<List<Calculation_inventoryDto>>(export_data);
               List<Calculation_inventoryDto> list = new 


List<Calculation_inventoryDto>();

               foreach (var tt in parentFields)
               {
     
                   dt.Columns.Add(tt.FieldDesc);//加载表头数据
               }

               foreach (var item in selectdata)
               {
                                     //加载表格数据
                   DataRow row = dt.NewRow();                   
                   row["产品编码"] = item.Product_No.ToString();
                   row["产品名称"] = item.Product_Name.ToString();
                   row["系列名称"] = item.Series_Name.ToString();
                   row["颜色"] = item.Color.ToString();
                   row["单位"] = item.Unit.ToString();
                   row["库存单价"] = item.Inventory_Unit_Price.ToString();                 
                   row["库存数量"] = item.Inventory_Num.ToString();
                   row["安全库存"] = item.Safe_Num.ToString();
                   row["订单数量"] = item.Sales_Num.ToString();
                   row["订单缺货数量"] = item.OOS_Num.ToString();
                   row["在途数量"] = item.OnLoad_Num.ToString();
                   row["需补货量"] = item.Replenishment_Num.ToString();
                   row["产品经理"] = item.PM_Person_Name.ToString();                   
                   row["近一月销量"] = item.Recent_Month_Sales_Num.ToString();
                   row["上月销量"] = item.Pre_Month_Sales_Num.ToString();
                   row["上月日销量"] = item.Pre_Month_Avg_Sales_Num.ToString();
                   dt.Rows.Add(row);
               }
               ```

你可能感兴趣的:(c#)