日期:2014-05-20  浏览次数:20779 次

linq to DataTable有些行有些不行?
C# code
  public static DataTable CopyToDataTable<T>( IEnumerable<T> array)
        {
            var ret = new DataTable();
            foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
                // if (!dp.IsReadOnly)
                ret.Columns.Add(dp.Name, dp.PropertyType);
            foreach (T item in array)
            {
                var Row = ret.NewRow();
                foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
                    // if (!dp.IsReadOnly)
                    Row[dp.Name] = dp.GetValue(item);
                ret.Rows.Add(Row);
            }
            return ret;
        } 

这方法来自网上.应该很多人用的.
到转换这句话就出错了. foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
调用:
  DataClasses1DataContext w1 = new DataClasses1DataContext();
  DataTable w3 = CopyToDataTable<b1>(w1.b1.ToList());
调试时.id列就能转换了.age列就不能转.这是怎么回事.
C# code
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_id", AutoSync=AutoSync.OnInsert, DbType="BigInt NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
        public long id
        {
            get
            {
                return this._id;
            }
            set
            {
                if ((this._id != value))
                {
                    this.OnidChanging(value);
                    this.SendPropertyChanging();
                    this._id = value;
                    this.SendPropertyChanged("id");
                    this.OnidChanged();
                }
            }
        }
        
        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_age", DbType="BigInt")]
        public System.Nullable<long> age
        {
            get
            {
                return this._age;
            }
            set
            {
                if ((this._age != value))
                {
                    this.OnageChanging(value);
                    this.SendPropertyChanging();
                    this._age = value;
                    this.SendPropertyChanged("age");
                    this.OnageChanged();
                }
            }
        }




------解决方案--------------------
Try:

http://blog.csdn.net/q107770540/article/details/6556210