ActiveRecord 复合主键时 实体转换为DataRow

    由于以前已经写好了的函数需要DataRow 类型的数据,故需要把实体转换为DataRow。

实体是由工具生成的,复合主键被处理成ID,如下:

        /// <summary>
        /// 主键ID
        /// </summary>
        [DataMember]
        [CompositeKey()]
        public virtual DZ_ZJJCSJCompositeKey ID
        {
            get;
            set;
        }

复合主键的定义如下:

[DataContract]
    [Serializable()]
    public partial class DZ_ZJJCSJCompositeKey
    {
        private int _Qydm;
        private int _Gjdm;
        private int _Yqtdm;
        private int _Qkdm;
        private String _Jh;
        /// <summary>
        /// 企业代码
        /// </summary>
        [DataMember]
        [KeyProperty(Column = "QYDM", ColumnType = "Int32")]
        public virtual int QYDM
        {
            get { return this._Qydm; }
            set { this._Qydm = value; }
        }
        /// <summary>
        /// 国家代码
        /// </summary>
        [DataMember]
        [KeyProperty(Column = "GJDM", ColumnType = "Int32")]
        public virtual int GJDM
        {
            get { return this._Gjdm; }
            set { this._Gjdm = value; }
        }
        /// <summary>
        /// 油气田代码
        /// </summary>
        [DataMember]
        [KeyProperty(Column = "YQTDM", ColumnType = "Int32")]
        public virtual int YQTDM
        {
            get { return this._Yqtdm; }
            set { this._Yqtdm = value; }
        }
        /// <summary>
        /// 区块代码
        /// </summary>
        [DataMember]
        [KeyProperty(Column = "QKDM", ColumnType = "Int32")]
        public virtual int QKDM
        {
            get { return this._Qkdm; }
            set { this._Qkdm = value; }
        }
        /// <summary>
        /// 井号
        /// </summary>
        [DataMember]
        [KeyProperty(Column = "JH", ColumnType = "String")]
        public virtual String JH
        {
            get { return this._Jh; }
            set { this._Jh = value; }
        }
        public override string ToString()
        {
            return String.Join(":", new string[] {
                                this._Qydm.ToString(),this._Gjdm.ToString(),this._Yqtdm.ToString(),this._Qkdm.ToString(),this._Jh.ToString()
                                       });
        }
        public override bool Equals(object obj)
        {
            if ((obj == this))
            {
                return true;
            }
            if (((obj == null)
                        || (obj.GetType() != this.GetType())))
            {
                return false;
            }
            DZ_ZJJCSJCompositeKey test = ((DZ_ZJJCSJCompositeKey)(obj));
            return (((_Qydm == test._Qydm) || ((_Qydm != null) && _Qydm.Equals(test._Qydm))) && ((_Gjdm == test._Gjdm) || ((_Gjdm != null) && _Gjdm.Equals(test._Gjdm))) && ((_Yqtdm == test._Yqtdm) || ((_Yqtdm != null) && _Yqtdm.Equals(test._Yqtdm))) && ((_Qkdm == test._Qkdm) || ((_Qkdm != null) && _Qkdm.Equals(test._Qkdm))) && ((_Jh == test._Jh) || ((_Jh != null) && _Jh.Equals(test._Jh))));
        }
        public override int GetHashCode()
        {
            return XorHelper(_Jh.GetHashCode(), XorHelper(_Qkdm.GetHashCode(), XorHelper(_Yqtdm.GetHashCode(), XorHelper(_Gjdm.GetHashCode(), _Qydm.GetHashCode()))));
        }
        private int XorHelper(int left, int right)
        {
            return left ^ right;
        }
    }

这时用原来的反射方法已经不行了 !

研究了两个小时才得到如下的代码,故赶紧存一下,也给有类似需要的人分享一下:

   /// <summary>
        ///
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public static DataRow ToDataRow(T instance)
        {
            if (instance == null)
                return null;
            Type type = typeof(T);
            DataTable table = new DataTable();
            PropertyInfo[] fields = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
            string temporary = string.Empty;
            Type temp = null;
            foreach (PropertyInfo field in fields)
            {
                temp = field.PropertyType;
                if (temp.FullName.StartsWith("System"))
                {
                    table.Columns.Add(field.Name);
                }
                else
                {
                    PropertyInfo[] pis = temp.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    foreach (PropertyInfo pi in pis)
                        table.Columns.Add(pi.Name);
                }
            }
            DataRow row = table.NewRow();
            foreach (PropertyInfo field in fields)
            {
                temp = field.PropertyType;
                if (temp.FullName.StartsWith("System"))
                {
                    row[field.Name] = field.GetValue(instance, null);
                }
                else
                {
                    PropertyInfo[] pis = temp.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    var vars = type.GetProperty(field.Name).GetValue(instance, null);
                    foreach (PropertyInfo pi in pis)
                    {
                        row[pi.Name] = pi.GetValue(vars, null);
                    }
                }
            }
            return row;
        }

你可能感兴趣的:(ActiveRecord)