Home  >  Article  >  Backend Development  >  C# DataRow comparison

C# DataRow comparison

黄舟
黄舟Original
2018-05-15 11:15:252888browse

<pre class="brush:php;toolbar:false;">  /// <summary>
        /// dataRow比较
        /// </summary>
        /// <param name="drA"></param>
        /// <param name="drB"></param>
        /// <param name="columnNames">需要比较的列名称</param>
        /// <returns></returns>
        public static bool DataRowCompare(DataRow drA, DataRow drB, string[] columnNames)
        {
            bool flag = false;
            //DataRow 中需要比较的列排序
            ColumnSort(drA, columnNames);
            ColumnSort(drB, columnNames);
            foreach (DataColumn dcA in drA.Table.Columns)
            {
                if (columnNames.Contains(dcA.ColumnName))
                {
                    foreach (DataColumn dcB in drB.Table.Columns)
                    {
                        if (columnNames.Contains(dcB.ColumnName))
                        {
                            if (dcB.ColumnName == dcA.ColumnName)//列名比较
                            {
                                //类型比较
                                if (dcB.DataType != dcA.DataType)
                                {
                                    flag = false;
                                    break;
                                }
                                //值比较
                                else if (CompareObject(drA[dcB.ColumnName], drB[dcB.ColumnName]))
                                {
                                    flag = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            return flag;
        }
		     /// <summary>
        /// 按照数组中列名顺序排序
        /// </summary>
        /// <param name="drA"></param>
        /// <param name="columnNames">按照数组中列名顺序排序</param>
        public static void ColumnSort(DataRow drA, string[] columnNames)
        {
            //drA 排序
            int i = 0;
            foreach (string columnName in columnNames)
            {
                if (drA.Table.Columns.Contains(columnName))
                {
                    drA.Table.Columns[columnName].SetOrdinal(i);
                    i++;
                }
            }
        }


Object comparison method CompareObject(object objA, object objB):

C# object comparison (value type, reference type)

Deep copy part of the code:

##C# Entity class serialization and deserialization One (XmlSerializer)

C# Entity class serialization and deserialization Two (DataContractSerializer)

The above is the comparison of C# DataRow Content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn