DataTable转成List
DataTable转成List
//把一个Datatable 赋值给一个List对象
//定义一个转换类
public class ConvertTool
{
public static List<T> DataConvert<T>(DataTable tb)
{
List<T> lst = new List<T>();
DataConvert<T>(tb, ref lst);
return lst;
} public static List<T> DataConvert<T>(DataTable tb, ref List<T> lst)
{
for (int i = ; i < tb.Rows.Count; i++)
{
lst.Add(DataConvert<T>(tb.Rows[i]));
}
return lst;
}
public static T DataConvert<T>(DataRow row)
{
//泛型 根据传来的类型创建实例对象
var type = typeof(T);
object obj = type.Assembly.CreateInstance(type.FullName);
var c = (T)obj;
DataConvert(row, ref c);
return c;
}
//获取一个类对象的所有公共属性 遍历所有属性 并赋值
public static T DataConvert<T>(DataRow row, ref T t) {
var ps = t.GetType().GetProperties();
var tbColumns = row.Table.Columns;
foreach (var c in ps)
{
var colName = c.Name;
if (tbColumns.Contains(colName))
{
object nr = row[colName] == DBNull.Value ? null : row[colName];
if (nr == null)
{
c.SetValue(t, nr, null);
}
else
{
var nrType = c.PropertyType;
if (nrType == typeof(decimal) || nrType == typeof(decimal?))
{
nr = Convert.ToDecimal(nr);
}
else if (nrType == typeof(Int64) || nrType == typeof(Int64?))
{
nr = Convert.ToInt64(nr);
}
else if (nrType == typeof(double) || nrType == typeof(double?))
{
nr = Convert.ToDouble(nr);
}
else if (nrType == typeof(Int32) || nrType == typeof(Int32?))
{
nr = Convert.ToInt32(nr);
}
else if (nrType == typeof(Int16) || nrType == typeof(Int16?))
{
nr = Convert.ToInt16(nr);
}
c.SetValue(t, nr, null);
}
}
}
return t;
}
}
定义一个测试的实体类
多种类型全部用上
public class TestClass
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Money { get; set; }
public bool IsValid { get; set; }
public DateTime BirthDay { get; set; } }
方法调用及显示结果
public class Program
{
static void Main(string[] args)
{
//定义一个DataTable 并赋值
DataTable dt = new DataTable("dtTest");
DataColumn dc = null;
dc = dt.Columns.Add("Id", Type.GetType("System.Int32"));
dc = dt.Columns.Add("Name", Type.GetType("System.String"));
dc = dt.Columns.Add("Money", Type.GetType("System.Decimal"));
dc = dt.Columns.Add("IsValid", Type.GetType("System.Boolean"));
dc = dt.Columns.Add("BirthDay", Type.GetType("System.DateTime"));
DataRow newRow;
newRow = dt.NewRow();
newRow["Id"] = "";
newRow["Name"] = "测试1";
newRow["IsValid"] = true;
newRow["Money"] = 100.00m;
newRow["BirthDay"] = DateTime.Now ;
dt.Rows.Add(newRow); newRow = dt.NewRow();
newRow["Id"] = "";
newRow["Name"] = "测试2";
newRow["IsValid"] = true;
newRow["Money"] = 100.00m;
newRow["BirthDay"] = DateTime.Now;
dt.Rows.Add(newRow); //调用DataTable转成List
List<TestClass> Li = ConvertTool.DataConvert<TestClass>(dt);
}
}
现在有很多流行的ORM框架,直接取出来对象数据很方便,不用再转。实现原理大同小异;
DataTable转成List的更多相关文章
- DataTable 转换成 Json的3种方法
在web开发中,我们可能会有这样的需求,为了便于前台的JS的处理,我们需要将查询出的数据源格式比如:List<T>.DataTable转换为Json格式.特别在使用Extjs框架的时候,A ...
- DataTable转换成IList<T>的简单实现
DataTable的无奈 很多时候,我们需要去操作DataTable.但DataTable的操作,实在是太不方便了.Linq?lambda表达式?统统没有... 特别是对现有结果集做进一步筛选,这样的 ...
- asp.net dataTable转换成Json格式
/// <summary> /// dataTable转换成Json格式 /// </summary> /// <param name="dt"> ...
- 将DataTable转换成CSV文件
DataTable用于在.net项目中,用于缓存数据,DataTable表示内存中数据的一个表.CSV文件最早用在简单的数据库里,由于其格式简单,并具备很强的开放性,所以起初被扫图家用作自己图集的标记 ...
- C#:DataTable映射成Model
这是数据库开发中经常遇到的问题,当然,这可以用现成的ORM框架来解决,但有些时候,如果DataSet/DataTable是第三方接口返回的,ORM就不方便了,还得自己处理. 反射自然必不可少的,另外考 ...
- 将DataSet(DataTable)转换成JSON格式(生成JS文件存储)
public static string CreateJsonParameters(DataTable dt) { /**/ /**/ /**/ /* /*********************** ...
- C#将DataTable转换成list的方法
本文实例讲述了C#将DataTable转换成list及数据分页的方法.分享给大家供大家参考.具体如下: /// <summary> /// 酒店评论列表-分页 /// </su ...
- DataTable转换成List<T>
很多时候需要将DataTable转换成一组model,直接对model执行操作会更加方便直观. 代码如下: public static class DataTableToModel { public ...
- C# 中DataTable转成模型List
C# 中DataTable转成模型List 引入using System.Reflection; 命名空间 使用注意实体类的属性名必须和DataTable的列名一致 使用: DBList<Sto ...
- ASP.NET 将DataTable解析成JSON简介
这里解析json使用的是Newtonsoft.Json.dll程序集.下面请看code: using System; using System.Collections.Generic; using S ...
随机推荐
- Java编程思想:擦除的神秘之处
import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; public class Test ...
- 自定义new和delete
#include "stdafx.h" #include <stdlib.h> #include <malloc.h> #include <iostr ...
- Javascript中style,currentStyle和getComputedStyle的区别以及获取css操作方法
style: 只能获取行内style. 调用:obj.style.属性; 兼容:都兼容 currentStyle: 可以获取该obj所有style,但只可读. 调用:obj.currentStyle[ ...
- [leetcode] #112 Path Sum (easy)
原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...
- 蓝牙Beacon广播数据包格式以及解析
目录 1. 获取原始蓝牙广播包 2. 安装WireShark软件 3. 分析Beacon广播包数据 3.1 第一个数据包格式 3.2 第二个数据包格式 3.3 Android程序开发中的蓝牙广播包 4 ...
- 《C#从入门到精通(第3版)》目录
C#从入门到精通(第3版)pdf+源码 一.基础知识 1.初识C#及其开发环境 2.开始C#之旅 3.变量与常量 4.表达式与运算符 5.字符与字符串 6.流程控制语句 7.数组与集合 8.属性和方法 ...
- SQLyog 破解版
百度云:链接:http://pan.baidu.com/s/1eSMEzIE 密码:ubi2
- codemirror使用
JS使用 使用bower下载 bower i codemirror 引入样式文件 <link rel="stylesheet" type="text/css&quo ...
- ssm下的CURD
https://github.com/MenghuiLiu/ssm-curd 以后有更新.... 照着前辈的足迹向前撸
- Codeforces1144B(B题)Parity Alternated Deletions
B. Parity Alternated Deletions Polycarp has an array aa consisting of nn integers. He wants to play ...