在List<T>转DataTable的时候 提示DataSet不支持System.nullable<>
请问怎么解决?
请问怎么解决?
解决方案 »
- c# 改变字符串字体的样式
- 分离sql server数据库,跑哪里去了。分离后的.mdf数据库里面的数据会不会改变??
- C# 对oralce数据库的备份和还原 在线等
- C# panel像googleMap那样拖动?
- 大家帮忙看看这是什么字体?
- 请高手来看下
- 如何单击GridView中DataField字段,运行服务器cs文件中函数(不是响应客户端JS)?
- 在Oracle数据库中,如何实现给定一个数据库连接得到数据库下所有的数据表!up又分!
- 带参数的Sql语句的问题 在线等 很急 麻烦大哥们解决下
- 关于CheckBox控件,这段代码怎么写?
- asp.net开发的项目发布到服务器上不能保存数据了 请帮忙看下
- web.comfig中给文件加密问题(在线等)
{
//创建属性的集合
List<PropertyInfo> pList = new List<PropertyInfo>();
Type type = typeof(TResult);
DataTable dt = new DataTable();
Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p);dt.Columns.Add(p.Name, p.PropertyType); });
foreach (var item in value)
{
//创建一个DataRow实例
DataRow row = dt.NewRow();
pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
dt.Rows.Add(row);
}
return dt;
}
}
空dbnull.value
或者赋值时用 row["col"] = obj ?? ""; 如果为null则用空代替。
或者赋值时用 row["col"] = obj ?? ""; 如果为null则用空代替。其实我就是不知道这边是怎么处理?
盼指点!
#region ListToDataTable
/// <summary>
/// ListToDataTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static DataTable ToDataTable<T>(this IEnumerable<T> list)
{
List<PropertyInfo> pList = new List<PropertyInfo>();
Type type = typeof(T);
DataTable dt = new DataTable();
Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
foreach (var item in list)
{
DataRow row = dt.NewRow();
pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
dt.Rows.Add(row);
}
return dt;
}
#endregion
可以参考一下
在表里面没有空值的时候
只是有了空值就提醒DatSet不支持System.nullable<>
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 });
*
*/
你已经有了一个数据集合,非要用List<>吗