List转DataTable
一、简单讲解
结合图片简单讲解DataTable几个比较重要的属性以便于更好的理解代码。
Table:表示一个表;
Rows:表行的集合;
Columns:表列的集合;
单元格是表格中行与列的交叉部分;
Rows[index]:index代表下标,从0开始;可以定位到第几行。
比如
Rows[0],代表第一行;
Rows[1],代表第二行;
Columns[index]:index代表下标,从0开始;可以定位到第几列。
Columns[0],代表第一列;
Columns[1],代表第二列;
想要获取到第几行第几列的数据我们有两种方法:
1) rows[行数][列数];
2) rows[行数][列名];
按照上图一的标记
想要获取标记为①的单元格值103
var value = rows[3][3]; 第4行第4列单元格值:
var value= rows[3]["MemberId"];第4行列名为MemberId的单元格值:
想要获取标记为②的单元格值CS202206271430001:
var value = rows[1][7]; 第2行第8列单元格值:
var value= rows[1]["UsageBillNo"];第2行列名为UsageBillNo的单元格值:
二、List转DataTable 代码如下
/// <summary> /// 测试入口 /// 模拟数据 /// </summary> /// <returns></returns> public DataTable ListToDataTableTest() { var couponUsageList = new List<CrmCouponTestDto>() { new CrmCouponTestDto { Id=1, CouponCode="test001", CouponId = 1, MemberId=100, IssueTime=Convert.ToDateTime("2022-06-27 14:00:00"), UsageTime=Convert.ToDateTime("3000-12-31 00:00:00"), UsageShopId=0, UsageBillNo="", EffectiveStart=Convert.ToDateTime("2022-06-27 14:00:00"), EffectiveEnd=Convert.ToDateTime("2023-06-27 14:00:00"), Status=0 }, new CrmCouponTestDto { Id=2, CouponCode="test002", CouponId = 1, MemberId=101, IssueTime=Convert.ToDateTime("2022-06-27 14:00:00"), UsageTime=Convert.ToDateTime("2022-06-27 14:30:00"), UsageShopId=2, UsageBillNo="CS202206271430001", EffectiveStart=Convert.ToDateTime("2022-06-27 14:00:00"), EffectiveEnd=Convert.ToDateTime("2023-06-27 14:00:00"), Status=1 }, new CrmCouponTestDto { Id=3, CouponCode="test003", CouponId = 1, MemberId=102, IssueTime=Convert.ToDateTime("2022-06-27 14:00:00"), UsageTime=Convert.ToDateTime("3000-12-31 00:00:00"), UsageShopId=0, UsageBillNo="", EffectiveStart=Convert.ToDateTime("2022-06-27 14:00:00"), EffectiveEnd=Convert.ToDateTime("2023-06-27 14:00:00"), Status=0 }, new CrmCouponTestDto { Id=4, CouponCode="test004", CouponId = 1, MemberId=103, IssueTime=Convert.ToDateTime("2022-06-27 14:00:00"), UsageTime=Convert.ToDateTime("3000-12-31 00:00:00"), UsageShopId=0, UsageBillNo="", EffectiveStart=Convert.ToDateTime("2022-06-27 14:00:00"), EffectiveEnd=Convert.ToDateTime("2023-06-27 14:00:00"), Status=0 } }; return ListToDataTable<CrmCouponTestDto>(couponUsageList); } /// <summary> /// 将List转化为DataTable核心方法 /// </summary> /// <returns></returns> public DataTable ListToDataTable<T>(List<T> data) { #region 创建一个DataTable,以实体名称作为DataTable名称 var tableName = typeof(T).Name; DataTable dt = new DataTable { TableName = tableName }; #endregion #region 拿取列名,以实体的属性名作为列名 var properties = typeof(T).GetProperties(); foreach (var item in properties) { var curFileName = item.Name; dt.Columns.Add(curFileName); } #endregion #region 列赋值 foreach (var item in data) { DataRow dr = dt.NewRow(); var columns = dt.Columns; var curPropertyList=item.GetType().GetProperties(); foreach (var p in curPropertyList) { var name = p.Name; var curValue = p.GetValue(item); int i = columns.IndexOf(name); dr[i] = curValue; } dt.Rows.Add(dr); } #endregion return dt; } /// <summary> /// 实体 /// </summary> public class CrmCouponTestDto { /// <summary> /// ID /// </summary> public long Id { get; set; } /// <summary> /// 卡券号 /// </summary> public string CouponCode { get; set; } /// <summary> /// 卡券ID /// </summary> public int CouponId { get; set; } /// <summary> /// 会员ID /// </summary> public int MemberId { get; set; } /// <summary> /// 发放时间 /// </summary> public DateTime IssueTime { get; set; } /// <summary> /// 使用时间 /// </summary> public DateTime UsageTime { get; set; } /// <summary> /// 使用店铺ID /// </summary> public int UsageShopId { get; set; } /// <summary> /// 使用单号 /// </summary> public string UsageBillNo { get; set; } /// <summary> /// 有效开始时间 /// </summary> public DateTime EffectiveStart { get; set; } /// <summary> /// 有效结束时间 /// </summary> public DateTime EffectiveEnd { get; set; } /// <summary> /// 状态 /// CouponStatus 卡券状态: /// -1:未领用 /// 0:未使用 /// 1:已使用 /// 2:已过期 ///3:已作废 ///4:转赠中 /// </summary> public Int16 Status { get; set; } }