泛型List集合转化为DateTable
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; namespace coDEalers
{
public static class Extension
{
public static DataTable ListToDataTable<T>(this IList<T> data, string tableName)
{
DataTable table = new DataTable(tableName); //special handling for value types and string
if (typeof(T).IsValueType || typeof(T).Equals(typeof(string)))
{ DataColumn dc = new DataColumn("Value");
table.Columns.Add(dc);
foreach (T item in data)
{
DataRow dr = table.NewRow();
dr[] = item;
table.Rows.Add(dr);
}
}
else
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
foreach (PropertyDescriptor prop in properties)
{
table.Columns.Add(prop.Name,
Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
{
try
{
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
}
catch (Exception ex)
{
row[prop.Name] = DBNull.Value;
}
}
table.Rows.Add(row);
}
}
return table;
}
}
}
上面的是方法,下面是调用的方法
DataTable dt = null;
List<StateList> stateListObj = JsonConvert.DeserializeObject<List<StateList>>(hdn_stateDetails_JSON.Value);
dt = stateListObj.ListToDataTable<StateList>("dtState");
这是个通用的方法,只要将需要转换的集合传进来就行了
泛型List集合转化为DateTable的更多相关文章
- 译:泛型List集合转化为DateTable的扩展方法
译文出处:http://www.codeproject.com/Tips/867866/Extension-Method-for-Generic-List-Collection-to-Da 这段代码是 ...
- C# 将list<>泛型集合 转化为 DataTable
使用案例:将页面easy ui 中datagrid表格中的数据,存成json字符串, 通过ajax和ashx传入C#将string类型的json字符串解析成list<>泛型集合, 由于业务 ...
- 泛型集合转化为DataTable
public class DataTableUtil { /// <summary> /// 泛型集合转化为dataTable /// </summary> /// <t ...
- .Net 笔记(二) 泛型和集合
前言: 本文中介绍 泛型和集合的区别.也算是自己的一个知识点的回顾,并且把它们写在自己的笔记中. 1.集合: 在讲到集合之前,我们先来回顾下数组的知识点吧,因为集合和数组的关系也是比较微妙的各有利弊, ...
- Spark:scala集合转化为DS/DF
scala集合转化为DS/DF case class TestPerson(name: String, age: Long, salary: Double) val tom = TestPerson( ...
- 牛客网Java刷题知识点之泛型概念的提出、什么是泛型、泛型在集合中的应用、泛型类、泛型方法、泛型接口、泛型限定上限、泛型限定下限、 什么时候使用上限?泛型限定通配符的体现
不多说,直接上干货! 先来看个泛型概念提出的背景的例子. GenericDemo.java package zhouls.bigdata.DataFeatureSelection; import ja ...
- 表达式树练习实践:C#值类型、引用类型、泛型、集合、调用函数
目录 表达式树练习实践:C#值类型.引用类型.泛型.集合.调用函数 一,定义变量 二,访问变量/类型的属性字段和方法 1. 访问属性 2. 调用函数 三,实例化引用类型 四,实例化泛型类型于调用 五, ...
- C# 篇基础知识11——泛型和集合
.NET提供了一级功能强大的集合类,实现了多种不同类型的集合,可以根据实际用途选择恰当的集合类型. 除了数组 Array 类定义在System 命名空间中外,其他的集合类都定义在System.Coll ...
- luogg_java学习_09_泛型_集合
这篇博客总结了半天,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 , 泛型 泛型介绍 1).类内部的属性的类型可以由外部决定: 2) ...
随机推荐
- lodash常用函数 - Array、Collection
lodash常用函数 - Array.Collection lodash版本 v3.10.1 1.Array.Collection pull 移除数组中满足条件的元素 var array = [1, ...
- mac phpstrom 环境配置
因为mac下自带php,但是没有环境(ini文件)所有需要自己重新安装一下: curl -s http://php-osx.liip.ch/install.sh | bash -s 5.5 # 5.5 ...
- devexpress 中 XtraTabcontrol 改变BackColor 的方法
装载自CSDN,原文地址 https://blog.csdn.net/zjzytnn/article/details/53699340 今天在实现CAD文件的读和显示操作的时候,需要将CAD文件显示 ...
- MUI实现上拉加载和下拉刷新
编写存储过程分页(此处使用T-SQL) CREATE PROC [dbo].[Common_PageList] ( @tab nvarchar(max),---表名 @strFld nvarchar( ...
- 超详细Redis入门教程【转】
这篇文章主要介绍了超强.超详细Redis入门教程,本文详细介绍了Redis数据库各个方面的知识,需要的朋友可以参考下 [本教程目录] 1.redis是什么 2.redis的作者何许人也 3.谁在使 ...
- %02hhX
大家经常会遇到将 调试信息例如从网络收到的数据包 或者 转换后的数据 打印出来,调试问题. 如果以ascii码打印的话,控制字符和ascii码以外的字符不能很好的查看具体值(看不到,或者乱码,尤其对于 ...
- PHP 字符串补0
转自:https://www.cnblogs.com/bluebirds/archive/2016/11/22/6091099.html#undefined 概述:项目中经常会使用到在一串编码左边.右 ...
- bzoj2336 [HNOI2011]任务调度
Description 正解:搜索+随机化. 先写个搜索,枚举所有没有要求的任务属于哪一种任务,然后再用爬山来更新最优解. 具体来说就是先把所有先做任务$A$的按照$a$时间从大到小排序,先做任务$B ...
- Fiddler学习基础(一)
Fiddler官方网站及下载地址:http://www.telerik.com/fiddler 1. Fiddler原理: 作为系统代理,所有的来自微软互联网服务(WinInet)的http请求再到达 ...
- selenium基础知识(概述、安装、IDE等)
参考http://www.yiibai.com/selenium/selenium_webdriver.html