泛型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) ...
随机推荐
- Linux ->> UBuntu 14.04 LTE下设置静态IP地址
UBuntu 14.04 LTE设置IP地址和一些服务器版本的Linux还不太一样.以Centos 7.0为例,网卡IP地址的配置文件应该是/etc/sysconfig/network-scripts ...
- How I explained OOD to my wife
Introduction My wife Farhana wants to resume her career as a software developer (she started her car ...
- InputStream中mark方法使用
在调用mark的地方做上标记,参数readlimit说明在读取readlimit个字符后书签做废(6.0好像没有失效 ,仍然可以标记位置),使用reset后回到标记的位置.import java.io ...
- 用CSS写扫描二维码图标
代码如下: <style>.icon{margin:300px;width:30px;height:30px;position:relative}.icon .b{border:2px s ...
- Python学习---重点模块之shelve
简单示例 import shelve f = shelve.open(r'shelve.txt') f['info'] = {'name':'ftl', 'age':23, 'sex': 'male' ...
- lua-excel助手
excel是我们工作及生活当中不可或缺的东西,好吧,我是一个游戏程序员,数值哥哥肯定会给我些表格的.回归正题,为什么需要做这个封装? 为什么需要这个项目,因为我们需要使用程序进行自动化操作 VBA我们 ...
- mongoDB学习之旅(一)
首先自己去https://www.mongodb.org/官网下载哦~ 我的是64位的 安装就简单讲下吧...下载个安装包, 随便安装, 只要你找到的... cmd进去你的安装目录下/bin文件夹.. ...
- 「C语言」在Windows平台搭建C语言开发环境的多种方式
新接触C语言,如何在Windows下进行C语言开发环境的搭建值得思考并整理. 以下多种开发方式择一即可(DEV C++无须环境准备). 注:本文知识来源于 Windows 平台搭建C语言集成开发环境 ...
- OpenXml操作Excel
要使用OpenXml首先要下载最新的Open XML Format SDK 2.0.具体的导入openxml的dll,去网上搜,很多 1.我个人写的XmlHelp类 using System; usi ...
- PHP数组和字符串相互转换以及判断字符串长度
这里只介绍最常用的方法: $array=explode(separator,$string); $string=implode(glue,$array); explode() 函数用来将字符串打散成数 ...