编程语言
首页 > 编程语言> > c# 构建一个DataTable数据集并把它转化为相应的类集合

c# 构建一个DataTable数据集并把它转化为相应的类集合

作者:互联网

示例:

        public void test()
        {
            DataTable dtz = new DataTable();
            //添加Table中的列
            DataColumn dc1 = new DataColumn("UserName", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("IDType", Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("IDCardNo", Type.GetType("System.String"));
            DataColumn dc4 = new DataColumn("SalaryAmount", Type.GetType("System.String"));
            dtz.Columns.Add(dc1);
            dtz.Columns.Add(dc2);
            dtz.Columns.Add(dc3);
            dtz.Columns.Add(dc4);
            //以上代码完成了DataTable的构架,但是里面是没有任何数据的
            for (int i = 0; i < 10; i++)
            {
                DataRow dr = dtz.NewRow();
                dr["UserName"] = "James"+i;
                dr["IDType"] = "1";
                dr["IDCardNo"] = "34212419800606" + i;
                dr["SalaryAmount"] = "5000";
                dtz.Rows.Add(dr);
            }
            var excelList = ModelConvertHelper<HRImportSalaryExcel>.ConvertToModel(dtz);

        }

定义一个类:

public class HRImportSalaryExcel
    {
        /// <summary>
        /// 姓名    
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// 证件类型   1- 身份证;2-护照;
        /// </summary>
        public string IDType { get; set; }
        /// <summary>
        /// 证件号码    
        /// </summary>
        public string IDCardNo { get; set; }
        /// <summary>
        /// 应发工资金额 
        /// </summary>
        public string SalaryAmount { get; set; }
}

DataTable转换为List

/// <summary>    
    /// 实体转换辅助类 DataTable转换到List  
    /// 使用方式
    ///把DataTable转换为IList<UserInfo>  
    ///IList<UserInfo> users = ModelConvertHelper<UserInfo>.ConvertToModel(dt);
    /// </summary>    
    public class ModelConvertHelper<T> where T : new()
    {
        public static List<T> ConvertToModel(DataTable dt)
        {
            // 定义集合    
            List<T> ts = new List<T>();

            // 获得此模型的类型   
            Type type = typeof(T);
            string tempName = "";

            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                // 获得此模型的公共属性      
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    tempName = pi.Name;  // 检查DataTable是否包含此列   
                    if (dt.Columns.Contains(tempName))
                    {
                        // 判断此属性是否有Setter      
                        if (!pi.CanWrite) continue;
                        object value = dr[tempName];
                        if (value != DBNull.Value)
                            pi.SetValue(t, value, null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }
    }

 

标签:c#,DataColumn,DataTable,dtz,构建,new,dr,public
来源: https://www.cnblogs.com/firstcsharp/p/16361412.html