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 @   LuoCore  阅读(1196)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示