Linq to DataTable 解决DataSet不支持System.nullable
来源:互联网 发布:linux 安全设置 编辑:程序博客网 时间:2024/05/17 23:58
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);
}
}
/*
* sample:
* var query = from ....;
* DataTable dt = query.ToDataTable(rec => new object[] { query });
*
*/
- Linq to DataTable 解决DataSet不支持System.nullable
- 解决DataSet不支持System.nullable
- LINQ to DataSet/DataTable
- LINQ to DataSet的DataTable操作
- LINQ系列:LINQ to DataSet的DataTable操作
- LINQ系列:LINQ to DataSet的DataTable操作
- Using LINQ to manipulate data in DataSet/DataTable
- 解决 LINQ to Entities 不支持 LINQ 表达式节点类型“Invoke”
- linq学习 Linq to DataSet
- LINQ(LINQ to DataSet)
- LINQ to DataSet
- LINQ to DataSet 【转载】
- LINQ To DataSet
- Linq to Dataset
- LINQ TO DataSet Overview
- linq to Dataset
- Linq To DataSet
- Linq to DataSet
- 谁知道索爱J105I怎么刷机
- Hadoop-0.20.1+windowsXP+Eclipse辛酸历程
- 由《社交网络》想到的...
- UIImageView动画:显示了something在屏幕上移动。
- Redis几个认识误区
- Linq to DataTable 解决DataSet不支持System.nullable
- 文件夹监视及文件删除的简易方法
- Redis 数据库官方文档
- Web前端国内的叫法与行业归类吗
- ubuntu install svn
- NoSQL空间
- UIImageView 拖动项目
- android常用控件(三)- ProgressBar、ListView
- DER编码和ASN.1