首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

linq to DataTable有些行有些不行?解决方法

2012-03-15 
linq to DataTable有些行有些不行?C# codepublic static DataTable CopyToDataTableT( IEnumerableT a

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

热点排行