译:泛型List集合转化为DateTable的扩展方法
译文出处:http://www.codeproject.com/Tips/867866/Extension-Method-for-Generic-List-Collection-to-Da
这段代码是能够帮助你把泛型集合List转出成DataTable的扩展方法。
背景:
不知道你是否知道这个扩展方法,但是你可以不做任何修改的去使用下面这个类的代码。
使用代码:
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;
}
}
}
优点(兴趣点):
这是一个把GenericList集合转化成DataTable的一个简单的方法。
用法:
DataTable dt = null;
List<StateList> stateListObj = JsonConvert.DeserializeObject<List<StateList>>(hdn_stateDetails_JSON.Value);
dt = stateListObj.ListToDataTable<StateList>("dtState");
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
译:泛型List集合转化为DateTable的扩展方法的更多相关文章
- 泛型List集合转化为DateTable
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; name ...
- C#中DataTable与实体集合通用转换(使用扩展方法)
本案例提供了:把DataRow转换为单个实体.dataTable转换为List泛型支持时间格式转换. 下文的方法都是扩展方法.扩展方法要求写在静态类中,方法也要静态. 它必须在一个非嵌套.非泛型的静态 ...
- C# 将list<>泛型集合 转化为 DataTable
使用案例:将页面easy ui 中datagrid表格中的数据,存成json字符串, 通过ajax和ashx传入C#将string类型的json字符串解析成list<>泛型集合, 由于业务 ...
- 泛型集合转化为DataTable
public class DataTableUtil { /// <summary> /// 泛型集合转化为dataTable /// </summary> /// <t ...
- .Net 笔记(二) 泛型和集合
前言: 本文中介绍 泛型和集合的区别.也算是自己的一个知识点的回顾,并且把它们写在自己的笔记中. 1.集合: 在讲到集合之前,我们先来回顾下数组的知识点吧,因为集合和数组的关系也是比较微妙的各有利弊, ...
- Java基础---泛型、集合框架工具类:collections和Arrays
第一讲 泛型(Generic) 一.概述 1.JDK1.5版本以后出现的新特性.用于解决安全问题,是一个类型安全机制. 2.JDK1.5的集合类希望在定义集合时,明确表明你要向集合中装入那种类 ...
- 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. 调用函数 三,实例化引用类型 四,实例化泛型类型于调用 五, ...
随机推荐
- scikit-learn主要模块和基本使用方法
从网上看到一篇总结的很不错的sklearn使用文档,备份勿忘. 引言 对于一些开始搞机器学习算法有害怕下手的小朋友,该如何快速入门,这让人挺挣扎的.在从事数据科学的人中,最常用的工具就是R和Pytho ...
- Web 开发常备工具
工欲善其事,必先利其器.如今 Web 开发标准越来越高,Web 开发者也在不断寻找途径提升自己的技能.为使大家的开发工作更顺利进行,本文整理了 10+ 款比较优秀的 Web 开发工具,希望对你有帮助. ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- Window Server IIS6.0部署webservice请求调用提示请求失败
在一台window server 2003机器上部署了一个webservice,iis为6.0,奇怪的是.在服务器本机调用webservice成功了,可是在其它client调用却反馈失败.详细什么原因 ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [GraphQL] Serve a GraphQL Schema as Middleware in Express
If we have a GraphQL Schema expressed in terms of JavaScript, then we have a convenient package avai ...
- [AX2012]Claims user
AX2012可以创建一种account type为claims user的账号,这种账号不需要在AD中事先已创建用户,但是claims账号是无法通过rich client登陆到AX,它的主要应用场景是 ...
- C# 文件操作方法
方法一: FileStream textFile = File.Open(@"F:\程序\新手测试\linqApplication1\linqApplication1\IO.txt" ...
- iis 访问网站需要进行身份验证
今天网站输入域名访问的时候提示需要输入账号密码,这是权限出了问题,百度了一下,解决了,分享一下: 1.登陆远程,右键我的电脑->管理->本地用户和组->用户,里面有一个IUSR_WD ...
- 构建基于WinRT的WP8.1 App 03:Page控件
单页面模板 通常利用Visual Studio 2013创建的最简单的WP8.1应用是Blank App,它只包含一个不带任何UI的页面,并且没有任何状态管理的逻辑. 该不带任何UI的页面称为Blan ...