当前位置:网站首页>List to table

List to table

2022-06-27 15:58:00 Yaoxiaodan

One 、 Simple explanation

Simply explain with pictures DataTable Several more important attributes to better understand the code .

     

Table: Represents a table ;
Rows: Set of table rows ;
Columns: Set of table columns ;

Cells are the intersection of rows and columns in a table ;

Rows[index]:index Subscript subscript , from 0 Start ; You can navigate to the line .

such as

Rows[0], That's the first row ;

Rows[1], Represents the second line ;

 

Row[Index]:Row When a row has been determined , Which column of this row

such as :

Row[0], It stands for the... Of this line 1 Column ;

Row[1], It stands for the... Of this line 2 Column ;

 

Columns[index]:index Subscript subscript , from 0 Start ; Which column can you navigate to .

Columns[0], For the first column ;

Columns[1], For the second column ;

 

There are two ways to get the data of rows and columns :

1) rows[ Row number ][ Number of columns ];

2) rows[ Row number ][ Name ];

 

According to the mark in Figure 1

Want to get marked as ① Cell value of 103

var value = rows[3][3]; The first 4 Xing di 4 Column cell values :

var value= rows[3]["MemberId"]; The first 4 The name of the line is MemberId Cell value of :

 

Want to get marked as ② Cell value of CS202206271430001:

var value = rows[1][7]; The first 2 Xing di 8 Column cell values :

var value= rows[1]["UsageBillNo"]; The first 2 The name of the line is UsageBillNo Cell value of : 

Two 、List turn DataTable The code is as follows  

        /// <summary>
        ///  Test entrance 
        ///  Analog data 
        /// </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>
        ///  take List Turn into DataTable The core approach 
        /// </summary>
        /// <returns></returns>
        public DataTable ListToDataTable<T>(List<T> data)
        {
            #region  Create a DataTable, Take the entity name as DataTable name 

            var tableName = typeof(T).Name;
            DataTable dt = new DataTable
            {
                TableName = tableName
            };

            #endregion

            #region  Take the column name , Take the attribute name of the entity as the column name        
                               
            var properties = typeof(T).GetProperties();
            foreach (var item in properties)
            {
                var curFileName = item.Name;
                dt.Columns.Add(curFileName);
            }

            #endregion

            #region  Column assignment 
            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>
    ///  Entity 
    /// </summary>
    public  class CrmCouponTestDto
    {
        /// <summary>
        /// ID
        /// </summary>
        public long  Id { get; set; }

        /// <summary>
        ///  Card number 
        /// </summary>     
        public string CouponCode { get; set; }

        /// <summary>
        ///  Card voucher ID
        /// </summary>
        public int CouponId { get; set; }

        /// <summary>
        ///  members ID
        /// </summary>
        public int MemberId { get; set; }

        /// <summary>
        ///  Time of issue 
        /// </summary>   
        public DateTime IssueTime { get; set; }

        /// <summary>
        ///  Use your time 
        /// </summary>      
        public DateTime UsageTime { get; set; }

        /// <summary>
        ///  Use the store ID
        /// </summary>      

        public int UsageShopId { get; set; }

        /// <summary>
        ///  Use order No 
        /// </summary>      
        public string UsageBillNo { get; set; }

        /// <summary>
        ///  Effective start time 
        /// </summary>      
        public DateTime EffectiveStart { get; set; }

        /// <summary>
        ///  Effective end time 
        /// </summary>      
        public DateTime EffectiveEnd { get; set; }

        /// <summary>
        ///  state 
        /// CouponStatus  Card and coupon status :
        /// -1: Uncollected 
        /// 0: not used 
        /// 1: Already used 
        /// 2: Has expired 
        ///3: Has been invalidated 
        ///4: Under donation 
        /// </summary>
    
        public Int16 Status { get; set; }
    }
View Code

  

原网站

版权声明
本文为[Yaoxiaodan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206271524397997.html