在List<T>转DataTable的时候 提示DataSet不支持System.nullable<>
请问怎么解决?

解决方案 »

  1.   

    请使用基本数据类型,为空时用DBnull.Value
      

  2.   

    public static DataTable ToDataTable<TResult>(this IEnumerable<TResult> value) where TResult : class
      {
      //创建属性的集合
      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
      

  3.   

    你创建DataTable的DataColumn应该设为AllowDBNull=True。
    或者赋值时用 row["col"] = obj ?? ""; 如果为null则用空代替。
      

  4.   

    请使用基本数据类型,为空时用DBnull.Value你创建DataTable的DataColumn应该设为AllowDBNull=True。
    或者赋值时用 row["col"] = obj ?? ""; 如果为null则用空代替。其实我就是不知道这边是怎么处理?
    盼指点!
      

  5.   


            #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
    可以参考一下
      

  6.   

    以上的代码都能用
    在表里面没有空值的时候 
    只是有了空值就提醒DatSet不支持System.nullable<>
      

  7.   

    那就不要用DataSet、DataTable,你已经有List<T>集合了怎么处理不好啊...或者写一个to DataTable的转换器,又不麻烦...
      

  8.   

    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 }); 
     * 
    */
      

  9.   

    请使用基本数据类型,为空时用DBnull.Value其实我想知道 这话怎么理解? 我在哪里处理?
      

  10.   

    那你就用hastable
    你已经有了一个数据集合,非要用List<>吗