泛型的运用(用于查询数据后DataTable转实体类)
2019.8.14 更新
补全了DataTable转泛型集合的方法:
/// <summary>
/// DataTable转实体类集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt">DataTable数据集</param>
public static List<T> SetValueForListByData<T>(DataTable dt) where T : new()
{
List<T> List = new List<T>();
foreach (DataRow dr in dt.Rows)
{
T Entity = new T();
//这里是昨天就发在随笔里的方法,下翻就能看到,
SetValueForEntityByData(Entity, dr);
//下面一段是完整的赋值流程,与上面调用的方法二取一即可。
//PropertyInfo[] propertyInfo = Entity.GetType().GetProperties();
//Type type = Entity.GetType();
//foreach (PropertyInfo item in propertyInfo)
//{
// item.SetValue(Entity, Convert.ChangeType(dr[item.Name], item.PropertyType));
//}
List.Add(Entity);
}
return List;
}
在循环DataTable中的每一行时调用该方法,可避免实体类属性众多时手动赋值的麻烦。
1.有返回值版本,该版本需要注意的是,普通情况下的泛型没有构造器,无法直接通过new关键字实例化,所以需要在方法签名后面加上 where T : new(),为方法内的所有泛型变量添加一个空构造器,使之可以实例化。它最终的效果是实例化实体类并且赋值,然后return赋值结果。
public static T SetValueForEntityByData<T>(DataRow dr) where T : new()
{
T Entity = new T();
PropertyInfo[] propertyInfo = Entity.GetType().GetProperties();
Type type = Entity.GetType();
foreach (PropertyInfo item in propertyInfo)
{
item.SetValue(Entity, Convert.ChangeType(dr[item.Name], item.PropertyType));
}
return Entity;
}
2.无返回值版本,不用担心泛型的实例化,省略where,但是需要能理解引用类型与值类型在内存上的储存方式的区别。它最终的效果是为传入的实体类赋值,并且赋值结果不用经过return便可以体现。
public static void SetValueForEntityByData<T>(T Entity,DataRow dr)
{
Type type = typeof(T);
PropertyInfo[] propertyInfo = Entity.GetType().GetProperties();
foreach (PropertyInfo item in propertyInfo)
{
item.SetValue(Entity, Convert.ChangeType(dr[item.Name], item.PropertyType));
}
}
泛型的运用(用于查询数据后DataTable转实体类)的更多相关文章
- DataTable与实体类的转换
多年前写的DataTable与实体类的转换,已放github 阅读目录 介绍 起因 代码 UnitTest GitHub 介绍 很多年前一直使用Ado.net,后来慢慢转型到其他的orm,在转型过程中 ...
- .net 根据匿名类生成实体类,根据datatable生成实体类,根据sql生成实体类
在开发中可能会遇到这几种情况 1.EF或LINQ查询出来的匿名对象在其它地方调用不方便,又懒的手动建实体类 2.通过datatable反射实体需要先建一个类 ,头痛 3.通过SQL语句返回的实体也需要 ...
- C# DataTable 转 实体类
C# 中查询结果DataTable转实体类: 比如:List<RtmInterview> rtmList = GetDataById( id); public List<RtmInt ...
- 用DataTable填充实体类List
/// <summary> /// 用DataTable填充实体类List /// </summary> public static List<T> FillLis ...
- DataTable与实体类互相转换
/// <summary> /// DataTable与实体类互相转换 /// </summary> /// <typeparam name="T"& ...
- DataTable转实体类
/// <summary> /// DataTable与实体类互相转换 /// </summary> /// <typeparam name="T"& ...
- 【转】DataTable与实体类互相转换
原文地址:https://www.cnblogs.com/marblemm/p/7084797.html /// <summary> /// DataTable与实体类互相转换 /// & ...
- DataTable和实体类通过反射相互转换
using System.Runtime.Serialization; using System.Data; using System.Reflection; using System.Collect ...
- datatable与实体类之间相互转化的几种方法
#region DataTable转换成实体类 /// <summary> /// 填充对象列表:用DataSet的第一个表填充实体类 /// </summary> /// & ...
随机推荐
- shell 解析json
未完待续 ### 解析api json文件为csv文件 cd /api ` do id=$(echo ${i}|sed 's/.html//') echo -n "${id}|" ...
- Ubuntu 开机自动挂载磁盘
Ubuntu 磁盘挂载 1.使用命令查看分区 sudo fdisk -l 1 可以根据上图提供的磁盘信息确定想挂载的磁盘,以及确定挂载的位置. 我此次的目的是将/dev/sda2 磁盘挂载到/mnt/ ...
- W3C vs IE盒模型
今年4月份的一次面试,问到盒模型,是我第一次接触到盒模型,但当时不太明白,没有说清楚,后来查了下,但一知半解. 下面分享下,我对盒模型的理解: 盒模型,也就是box-sizing,分为content- ...
- 开源CMS比较
PHP-CMS的发展方向:简单,易用,美观 http://www.php-cms.cn/ 看点1,服务器一键安装,鼠标点点就搞定:输入数据库参数,在服务器上点一个按钮就完成全部的安装.简单配置一下网 ...
- pip 指定目录安装
pip install --target=d:\somewhere\other\than\the\default package_name
- SuperSocket AppServer 的两个事件: NewSessionConnected 和 SessionClosed
订阅事件: appServer.NewSessionConnected += new SessionHandler<AppSession>(appServer_NewSessionConn ...
- HTML--表格与表单
一.表格 <table></table>表格 width:宽度.可以用像素或百分比表示. 常用960像素. border:边框,常用值为0. cellpadding:内容跟边框 ...
- 2019-9-2-win10-uwp-车表盘-径向规
title author date CreateTime categories win10 uwp 车表盘 径向规 lindexi 2019-09-02 12:57:38 +0800 2018-2-1 ...
- jackson中的@JsonBackReference和@JsonManagedReference,以及@JsonIgnore
jackson中的@JsonBackReference和@JsonManagedReference,以及@JsonIgnore均是为了解决对象中存在双向引用导致的无限递归(infinite recur ...
- 2018-8-10-git-提交添加-emoij-文字
title author date CreateTime categories git 提交添加 emoij 文字 lindexi 2018-08-10 19:16:52 +0800 2018-2-1 ...