C# DataTable DataSet DataRow 转实体类集合,实体类和实体类集合转成DataTable 扩展方法分享
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C# DataTable DataSet DataRow 转实体类集合,实体类和实体类集合转成DataTable 扩展方法分享,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7422字,纯文字阅读大概需要11分钟。
内容图文
![C# DataTable DataSet DataRow 转实体类集合,实体类和实体类集合转成DataTable 扩展方法分享](/upload/InfoBanner/zyjiaocheng/590/aef184120a1e43088e580e76e65f310d.jpg)
C# DataTable DataSet DataRow 转实体类集合,实体类和实体类集合转成DataTable 扩展方法分享
代码越写越灵活,分享越分享越快乐
C# DataTable DataSet DataRow 转实体类集合,实体类和实体类集合转成DataTable 扩展方法分享
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Reflection;namespace System { /// /// DataTable转list泛型集合 /// public static class DataTableExtend { /// /// DataTable转换List /// /// 泛型 /// DataTable /// List泛型集合 public static ListToList(this DataTable dt) where T : class, new() { var list = new List(); foreach (DataRow dr in dt.Rows) { //泛型对象 T model = dr.ToDataRowModel(); list.Add(model); } return list; } /// /// DataSet转换List /// /// 泛型 /// DataTable /// List泛型集合 public static ListToList(this DataSet ds) where T : class, new() { var list = new List(); list = ds.Tables[0].ToList(); return list; } /// /// DataRow转换T模型 /// /// 泛型 /// DataTable /// List泛型集合 public static T ToDataRowModel(this DataRow dr) where T : class, new() { //泛型对象 T model = new T(); //属性集合 var listPro = model.GetType().GetProperties().Where(item => !item.IsDefined(typeof(InternalAttribute), false)).ToArray(); foreach (PropertyInfo pi in listPro) { var columnName = pi.Name;//属性=字段 var columnType = pi.PropertyType;//属性类型 var underlyingtype = Nullable.GetUnderlyingType(columnType);//返回指定可以为null值的类型 //判断属性是否可以写入 if (!pi.CanWrite) continue; if (!dr.Table.Columns.Contains(columnName)) continue; var value = dr[columnName]; //判断字段值是否为空 if (value == DBNull.Value) continue; //根据属性类型转换数据库字段类型 if (columnType == typeof(string)) pi.SetValue(model, value.ToString(), null); else if (columnType == typeof(int) || columnType == typeof(int?)) pi.SetValue(model, Convert.ToInt32(value), null); else if (columnType == typeof(DateTime) || columnType == typeof(DateTime?)) pi.SetValue(model, Convert.ToDateTime(value), null); else if (columnType == typeof(decimal)) pi.SetValue(model, Convert.ToDecimal(value), null); else if (columnType == typeof(double)) pi.SetValue(model, Convert.ToDouble(value), null); else if (columnType == typeof(float)) pi.SetValue(model, Convert.ToSingle(value), null); else if ((underlyingtype ?? columnType).IsEnum) { if (underlyingtype != null && !string.IsNullOrEmpty(value.ToString())) pi.SetValue(model, Enum.Parse(underlyingtype ?? columnType, value.ToString()), null); else if (underlyingtype == null && !string.IsNullOrEmpty(value.ToString())) pi.SetValue(model, Convert.ToInt32(value), null); else pi.SetValue(model, -1, null); } else pi.SetValue(model, value, null); } return model; } } }
实体列表转换成DataTable
/// /// 实体列表转换成DataTable /// /// 实体 /// 实体列表 /// public static DataTable EntityListToDataTable(this IList entityList) where TEntity : class { if (entityList == null) return null; var dt = new DataTable(typeof(TEntity).Name); var myPropertyInfo = typeof(TEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance); #region 创建表结构 foreach (var property in myPropertyInfo) { Type colType = property.PropertyType; if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>)) { colType = colType.GetGenericArguments()[0]; var col = new DataColumn(property.Name, colType) { Caption = property.ToDescription() }; col.AllowDBNull = true; dt.Columns.Add(col); } else { var col = new DataColumn(property.Name, colType) { Caption = property.ToDescription() }; dt.Columns.Add(col); } } #endregion foreach (var entity in entityList) { if (entity == null) continue; var row = dt.NewRow(); foreach (var propertyInfo in myPropertyInfo) { if (propertyInfo.GetValue(entity, null) == null) row[propertyInfo.Name] = DBNull.Value; else row[propertyInfo.Name] = propertyInfo.GetValue(entity, null); } dt.Rows.Add(row); } return dt; }
实体转换成DataTable
/// /// 实体转换成DataTable /// Add by loki 20201011 /// /// 实体 /// public static DataTable EntityToDataTable(this TEntity entity) where TEntity : class { if (entity == null) return null; var dt = new DataTable(typeof(TEntity).Name); var myPropertyInfo = typeof(TEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance); #region 创建表结构 foreach (var property in myPropertyInfo) { Type colType = property.PropertyType; if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>)) { colType = colType.GetGenericArguments()[0]; var col = new DataColumn(property.Name, colType) { Caption = property.ToDescription() }; col.AllowDBNull = true; dt.Columns.Add(col); } else { var col = new DataColumn(property.Name, colType) { Caption = property.ToDescription() }; dt.Columns.Add(col); } } #endregion var row = dt.NewRow(); foreach (var propertyInfo in myPropertyInfo) { if (propertyInfo.GetValue(entity, null) == null) row[propertyInfo.Name] = DBNull.Value; else row[propertyInfo.Name] = propertyInfo.GetValue(entity, null); } dt.Rows.Add(row); return dt; }
你如果觉得有用就拿去不用谢!C# DataTable DataSet DataRow 转实体类集合,实体类和实体类集合转成DataTable 扩展方法分享,这里其实很简单,就是用到反射技术去实现的,将数据库表字段与C#实体属性进行反射
内容总结
以上是互联网集市为您收集整理的C# DataTable DataSet DataRow 转实体类集合,实体类和实体类集合转成DataTable 扩展方法分享全部内容,希望文章能够帮你解决C# DataTable DataSet DataRow 转实体类集合,实体类和实体类集合转成DataTable 扩展方法分享所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。