完美解决方案,可排除DATASET不支持System.Nullable错误
完美解决方案,可排除DATASET不支持System.Nullable错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;
using YTO.WeiXin.Model;
using System.Collections; namespace YTO.WeiXin.Core
{
public class ListToDataTable
{
public static DataTable ToDataTable<T>(IList<T> list)
{
return ToDataTable(list, null);
} //将list转换成DataTable
public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
{
List<string> propertyNameList = new List<string>();
if (propertyName != null)
{
propertyNameList.AddRange(propertyName);
}
DataTable result = new DataTable();
if (list.Count > )
{
PropertyInfo[] propertys = list[].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
//if (DBNull.Value.Equals(pi.PropertyType))
//{
// // pi.PropertyType = DateTime;
//}
Type colType = pi.PropertyType;
if (colType.IsGenericType && colType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
colType = colType.GetGenericArguments()[0];
}
result.Columns.Add(pi.Name,colType);
//result.Columns.Add(pi.Name, pi.PropertyType);
}
else
{
if (propertyNameList.Contains(pi.Name))
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
}
}
for (int i = ; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == )
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
else
{
if (propertyNameList.Contains(pi.Name))
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
}
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
完美解决方案,可排除DATASET不支持System.Nullable错误的更多相关文章
- 匿名类型 使用泛型T linq返回dynamic类型的匿名实体 如何把匿名类型.GetType()返回的对象传进泛型里面 EF实体查询出的数据List<T>转DataTable出现【DataSet 不支持 System.Nullable<>】的问题
[100分]紧急求助:LinQ下使用IQueryable<T>如何将返回类型<T>使用匿名类型 问题描述如下:我有一个方法如下:public IQueryable Dissen ...
- EF实体查询出的数据List<T>转DataTable出现【DataSet 不支持 System.Nullable<>】的问题
public static DataTable ToDataTable<T>(this IEnumerable<T> varlist, CreateRowDelegate< ...
- .NET领域最为流行的IOC框架之一Autofac WebAPI2使用Autofac实现IOC属性注入完美解决方案 AutoFac容器初步
.NET领域最为流行的IOC框架之一Autofac 一.前言 Autofac是.NET领域最为流行的IOC框架之一,微软的Orchad开源程序使用的就是Autofac,Nopcommerce开源程 ...
- asp.net2.0导出pdf文件完美解决方案【转载】
asp.net2.0导出pdf文件完美解决方案 作者:清清月儿 PDF简介:PDF(Portable Document Format)文件格式是Adobe公司开发的电子文件格式.这种文件格式与操作系统 ...
- No resource found that matches the given name 'Theme.AppCompat.Light 的完美解决方案
No resource found that matches the given name 'Theme.AppCompat.Light 的完美解决方案 首先这个问题的产生是由于缺少Theme.App ...
- Xcode6.1标准Framework静态库制作方法。工程转Framework,静态库加xib和图片。完美解决方案。
http://www.cocoachina.com/bbs/read.php?tid-282490.html Xcode6.1标准Framework静态库制作方法.工程转Framework,静态库加x ...
- c#如何在win7下设置IE代理的完美解决方案
有人还发现:在window7下, 在一个进程中, 设置和取消不能都执行,---- 要么设置,要么取消. 但如果第一次运行时,只进行设置代理,退出后再进运行,只进行取消,这是没有问题的. 简单说说中医 ...
- WebBrowser脚本错误的完美解决方案
原文:WebBrowser脚本错误的完美解决方案 当IE浏览器遇到脚本错误时浏览器,左下角会出现一个黄色图标,点击可以查看脚本错误的详细信息,并不会有弹出的错误信息框.当我们使用WebBrowse ...
- DevExpress控件cxGrid实现多列模糊匹配输入的完美解决方案
本方案不需要修改控件源码,是完美解决cxgrid或TcxDBExtLookupComboBox支持多列模糊匹配快速输入的最佳方案!! 转自https://blog.csdn.net/qq5643020 ...
随机推荐
- java网络编程socket解析
转载:http://www.blogjava.net/landon/archive/2013/07/02/401137.html Java网络编程精解笔记2:Socket详解 Socket用法详解 在 ...
- js数量添加(如购物车添加,删减数量)
<div class="fl shop-num ml24"><input type="button" style=" border- ...
- 抓包工具tshark使用备忘
抓包命令行工具tshark可以用于自定制,相比GUI工具可以实现一些自动化,譬如把某些关注的数据抓起下来存放到文本中,然后再分析输出. demo: std::string deco ...
- HDU 5768 中国剩余定理
题目链接:Lucky7 题意:求在l和r范围内,满足能被7整除,而且不满足任意一组,x mod p[i] = a[i]的数的个数. 思路:容斥定理+中国剩余定理+快速乘法. (奇+ 偶-) #incl ...
- PHP获取汉字的转化为拼音字母实现程序
一个完整的php获取汉字拼音字母的实现程序,有需要的朋友可参考一下. <?php class GetPingYing { private $pylist = array( 'a'=>-20 ...
- Iterator之ListIterator简介
ListIterator是什么? (参考自百度百科) java中的ListIterator在Iterator基础上提供了add.set.previous等对列表的操作.但是ListIterator跟I ...
- 关于HTML5应用开发功耗调优化小结
HTML5的优化一直是困扰我的难题,特别是在移动端开发游戏和应用,所以对此进行了一些总结: 功耗优化点介绍 在移动设备中主要的功耗点在: 1. 网络的传输, 不管是3G网络还是WiFi传输都是移动设备 ...
- 文件浏览器及数码相框 -2.3.2-freetype_arm-2
显示多行文字 两行文字左边对齐 简单使用两个循环显示两行字体 根据上一行字体的宽度来进行下一行左边的计算 #include <sys/mman.h> #include <sys/ty ...
- 内工大acm校赛--整理代码
题目:小明搜到一行无缩进无换行代码,请帮小明整理代码.无for语句和case语句,而且只有一个主函数.你只要控制注意“:”“{”“}”这三个符号带来的缩进和换行效果就行. Input: 输入只有一行, ...
- 初步比较zeromq vs. wcf
用最简单的Calculator比较zeromq的req-rep模式 vs. wcf的http和net.tcp模式,看哪一种的传输性能更高. 1.比较结果如下 方式 耗费时间 wcf_http_sing ...