解决DataSet不支持System.nullable
来源:互联网 发布:怎样制作一个软件 编辑:程序博客网 时间:2024/05/18 00:32
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.Linq;
using System.Xml.Linq;
namespace UserFunction
{
/// <summary>
/// Summary description for LinqToDataTable
/// </summary>
static public class LinqToDataTable
{
static public DataTable ToDataTable<T>(this IEnumerable<T> varlist, CreateRowDelegate<T> fn)
{
DataTable dtReturn = new DataTable();
// column names
PropertyInfo[] oProps = null;
// Could add a check to verify that there is an element 0
foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others will follow
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow(); foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
}
dtReturn.Rows.Add(dr);
}
return (dtReturn);
}
public delegate object[] CreateRowDelegate<T>(T t);
}
}
/*
* 示例:
* var query = from ....;
* DataTable dt = query.ToDataTable(rec => new object[] { query });
*
*/
- 解决DataSet不支持System.nullable
- Linq to DataTable 解决DataSet不支持System.nullable
- System.Nullable
- IList转DataSet(支持Nullable)
- IList转DataSet(支持Nullable)
- System.Nullable<T> int?
- List<> 转换为Dataset的C#代码实现 解决Nullable问题
- 可空类型 System.Nullable
- 可空类型的判断 System.Nullable
- System.Nullable<T> 的简单理解
- Nullable
- @Nullable
- System.Nullable<System.DateTime>”不包含“Year”的定义
- 判断一个类型是否为可空类型(System.Nullable)
- 类型转化方法(处理System.Nullable类型)
- 判断一个类型是否为可空类型(System.Nullable)
- 跨平台的 NodeJS 组件解决 .NetCore 不支持 System.Drawing图形功能的若干问题
- 解决服务器不支持json
- android文件操作
- Java NIO(1)
- 键盘按钮KeyCode大全
- maven的学习---聚合工程的创建(四)
- 借记卡、贷记卡、预付卡、准贷记卡、信用卡
- 解决DataSet不支持System.nullable
- canvas 学习 第一部份
- 链表
- intel gpu 解码
- 使用eclipse通过jboss开发简单的ejb应用(jboss7.x & ejb3.x)
- Lucene6入门教程(一)简介和学习流程
- hdu 6148 (数位dp)
- HDU 6143 Killer Names(容斥原理)
- js实现动图效果