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 ...
随机推荐
- 数据结构与算法---堆排序(Heap sort)
堆排序基本介绍 1.堆排序是利用堆这种数据结构而设计的一种排序算法,堆排序是一种选择排序,它的最坏,最好,平均时间复杂度均为O(nlogn),它也是不稳定排序. 2.堆是具有以下性质的完全二叉树:每个 ...
- NOIP2018普及T4暨洛谷P5018 对称二叉树题解
题目链接:https://www.luogu.org/problemnew/show/P5018 花絮:这道题真的比历年的t4都简单的多呀,而且本蒟蒻做得出t4做不出t3呜呜呜... 这道题可以是一只 ...
- 个人永久性免费-Excel催化剂功能第31波-数量金额分组凑数功能,财务表哥表姐最爱
在财务工作过程中,很大时候需要使用到凑数的需求,花了两三天时间认真研究了一下,本人水平也只能做代码搬运工,在用户体验上作了一下完善.完成了Excel版的凑数功能. 文章出处说明 原文在简书上发表,再同 ...
- 【HDOJ】1062 Text Reverse
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignati ...
- 每日一问:Android 消息机制,我有必要再讲一次!
坚持原创日更,短平快的 Android 进阶系列,敬请直接在微信公众号搜索:nanchen,直接关注并设为星标,精彩不容错过. 我 17 年的 面试系列,曾写过一篇名为:Android 面试(五):探 ...
- MySql(Linux)
百度云:链接:http://pan.baidu.com/s/1jHQtPau 密码:elr8 官方下载网址:http://dev.mysql.com/downloads/mysql/
- luogu题解 P3388 【【模板】割点(割顶)】
外加定义:在一个无向图中,如果删掉点 x 后图的连通块数量增加,则称点 x 为图的割点. 外加图示 开始思路为割桥上的点为割点,后来证明的确正确. 不过可惜的是他的逆定理错了(gg了),不过数据很弱以 ...
- php--学习封装类 (一)(操作mysql数据库的数据访问)
<?php class DBDA //定义一个类 { //定义成员变量,不能直接定义,前面要加上public或者是private public $host = "localhost&q ...
- java相关的一些资源
http://www.cnblogs.com/best/p/5876559.html#_label1
- Cookie和Session的使用详解
我们在使用接口请求时经常听到Cookie和Session的知识,那么它们的实际意义和使用场景在哪里呢 ? 介绍如下 一.首先需要了解的是为什么需要有Cookie和Session这两个东西:Htt ...