DataSet 反射转换成 List<T>
/// <summary>
/// DataSet转换成指定返回类型的实体集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ds"></param>
/// <returns></returns>
public static List<T> DataSetToList<T>(DataSet ds)
{
PropertyInfo[] properties = typeof(T).GetProperties();
List<T> list = new List<T>(); foreach (DataRow dr in ds.Tables[].Rows)
{
T temp = System.Activator.CreateInstance<T>();
foreach (PropertyInfo pro in properties)
{
int colIndex = ds.Tables[].Columns.IndexOf(pro.Name);
if (colIndex > -)
{
if (pro.PropertyType == typeof(String))
{
pro.SetValue(temp, dr[colIndex].ToString(), null);
}
else if (pro.PropertyType == typeof(int))
{
pro.SetValue(temp, (int)dr[colIndex], null);
}
else if (pro.PropertyType == typeof(long))
{
pro.SetValue(temp, (long)dr[colIndex], null);
}
}
}
list.Add(temp);
}
return list;
}
DataSet 反射转换成 List<T>的更多相关文章
- DataTabel DataSet 对象 转换成json
public class DataTableConvertJson { #region dataTable转换成Json格式 /// <summary> ...
- c#实现list,dataset,DataTable转换成josn等各种转换方法总和
using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Refle ...
- 使用linq对ado.net查询出来dataset集合转换成对象(查询出来的数据结构为一对多)
public async Task<IEnumerable<QuestionAllInfo>> GetAllQuestionByTypeIdAsync(int id) { st ...
- 把dataset对象转换成list集合方法
public static List<T> GetList<T>(DataTable table) where T:new() { List<T> list = n ...
- 将Xml字符串转换成(DataTable || DataSet || XML)对象
今天用到一个功能:就是把从数据库读出来的内容转换成XML字符串流格式,并输出给一个功能函数.在写的过程,为方便以后的使用,我对这一功能进行分装.该类的具体格式如下:XmlConvert类命名空间:Ni ...
- sparksql 用反射的方式将rdd转换成dataset/dataframe
java public class ReflectionDemo { private static SparkConf conf = new SparkConf().setAppName(" ...
- C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。
C#中对象,字符串,dataTable.DataReader.DataSet,对象集合转换成Json字符串方法. public class ConvertJson { #region 私有方法 /// ...
- .NET(C#)中的DataSet、string、DataTable等对象转换成Json
ConvertJson.cs类 using System; using System.Collections.Generic; using System.Text; using System.Data ...
- 利用反射将Datatable、SqlDataReader转换成List模型
1. DataTable转IList public class DataTableToList<T>whereT :new() { ///<summary> ///利用反射将D ...
随机推荐
- NLP第二课(搜索)
最近压力太大了,持续性修改0注释的代码,变量为阿拉伯数字的代码,压力山大,摆正心态,没有那些bug,还需要我们来做些什么呢?如果一个特别出色的项目,也体现不出来你个人的出色.几句牢骚,我们今天来继续说 ...
- C# 随机 抽奖 50个随机码 不重复
static List<int> Given50RandomNumbers() { List<int> intList = new List<int>(); for ...
- c# "As" 与 "Is"效率 (原发布csdn 2017-10-07 11:49:18)
十一长假就要过去了,今年假期没有回家,一个人闲着无聊就在看C#语言规范5.0中文版.昨天看了 is运算符和 as运算符,平时项目中也有用到这两种符号,对于其效率也没有进行比较过,趁着假期有空,先看下效 ...
- var变量
# Aduthor:CCIP-Ma name = "ma" name2 = name name = "ccip-ma" print("My name ...
- 大白话说GIT常用操作,常用指令git操作大全
列一下在开发中用的比较多的git指令 git clone https://github.com/chineseLiao/Small-career // 克隆远程仓库到本地 git add . // 把 ...
- 信息收集利器:ZoomEye
前言 ZoomEye是一款针对网络空间的搜索引擎,收录了互联网空间中的设备.网站及其使用的服务或组件等信息. ZoomEye 拥有两大探测引擎:Xmap 和 Wmap,分别针对网络空间中的设备及网站, ...
- ios基础视频
http://wenku.baidu.com/course/view/1ce3571252d380eb62946d8c?cid=502
- 第七篇Scrum冲刺博客
第七篇Scrum冲刺博客 一.站立式会议 提供当天站立式会议照片一张 二.每个人的工作 成员 已完成工作 明天计划完成的工作 遇到的困难 林剑峰 加入搜索页面 无 陆君健 校园卡匹配功能的实现 无 石 ...
- vue3.0创建项目和基本配置
借鉴博客:https://www.jianshu.com/p/6307c568832d/ https://www.cnblogs.com/KenFine/p/10850386.html 项目创建好后, ...
- Invalid utf8mb4 character string: '"'
将CSV格式数据导入 mysql,报错: load data infile 'f:/nmg.csv' into table nmg fields terminated by ',' optionall ...