C# Model对象转DataTable

https://blog.csdn.net/a11112244444/article/details/78921200

 

https://www.cnblogs.com/chongde/articles/5992040.html

 

  /// <summary>
        /// 实体类转换成DataTable
        /// </summary>
        /// <param name="modelList">实体类列表</param>
        /// <returns></returns>
        public DataTable FillDataTable(List<T> modelList)
        {
            if (modelList == null || modelList.Count == 0)
            {
                return null;
            }
            DataTable dt = CreateData(modelList[0]);
 
 
            foreach (T model in modelList)
            {
                DataRow dataRow = dt.NewRow();
                foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                {
                    dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
                }
                dt.Rows.Add(dataRow);
            }
            return dt;
        }
 
 
        /// <summary>
        /// 根据实体类得到表结构
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns></returns>
        private DataTable CreateData(T model)
        {
            DataTable dataTable = new DataTable(typeof(T).Name);
            foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
            {
                dataTable.Columns.Add(new DataColumn(propertyInfo.Name, propertyInfo.PropertyType));
            }
            return dataTable;
        }
————————————————
版权声明:本文为CSDN博主「张伟光」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/a11112244444/article/details/78921200

 

 // 当字段类型是Nullable<>时
41                         Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
42                         {
43 
44                             colType = colType.GetGenericArguments()[0];
45 
46                         }
47 
48                         dtReturn.Columns.Add(new DataColumn(pi.Name, colType));




自己测试:
  DataTable dataTable = new DataTable(typeof(OperationModel.ReturnSubsidiary.Business.OrderDetailByMainOrderInfo).Name);
            foreach (System.Reflection.PropertyInfo propertyInfo in typeof(OperationModel.ReturnSubsidiary.Business.OrderDetailByMainOrderInfo).GetProperties())
            {
                DataColumn dc = new DataColumn();
                dc.ColumnName = propertyInfo.Name;
                // 当字段类型是Nullable<>时
                Type colType = propertyInfo.PropertyType;
                if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                {

                    colType = colType.GetGenericArguments()[0];

                }
                
                dc.DataType = colType;
                dataTable.Columns.Add(dc);
            }
            DataRow dataRow = dataTable.NewRow();
            foreach (System.Reflection.PropertyInfo propertyInfo in typeof(OperationModel.ReturnSubsidiary.Business.OrderDetailByMainOrderInfo).GetProperties())
            {
                try
                {
                    dataRow[propertyInfo.Name] = propertyInfo.GetValue(a, null);
                }
                catch
                {
                    dataRow[propertyInfo.Name] = DBNull.Value;
                }
                
            }
            dataTable.Rows.Add(dataRow);


List:

 DataTable dataTable = new DataTable(typeof(OperationModel.ReturnSubsidiary.Business.OrderDetailByMainOrderInfo).Name);
          
            foreach (System.Reflection.PropertyInfo propertyInfo in typeof(OperationModel.ReturnSubsidiary.Business.OrderDetailByMainOrderInfo).GetProperties())
            {
                Type colType = propertyInfo.PropertyType;
                if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                {

                    colType = colType.GetGenericArguments()[0];

                }
                dataTable.Columns.Add(new DataColumn(propertyInfo.Name, colType));
            }

            foreach (OperationModel.ReturnSubsidiary.Business.OrderDetailByMainOrderInfo model in modelList)
            {
                DataRow dataRow = dataTable.NewRow();
                foreach (System.Reflection.PropertyInfo propertyInfo in typeof(OperationModel.ReturnSubsidiary.Business.OrderDetailByMainOrderInfo).GetProperties())
                {
                    try
                    {
                        dataRow[propertyInfo.Name] = propertyInfo.GetValue(model, null);
                    }
                    catch
                    {
                        dataRow[propertyInfo.Name] = DBNull.Value;
                    }
                 
                }
                dataTable.Rows.Add(dataRow);
            }


posted @ 2021-02-26 11:18  LuoCore  阅读(1179)  评论(0编辑  收藏  举报