Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataSet / System / Data / DataRowComparer.cs / 1305376 / DataRowComparer.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //[....] //spather //----------------------------------------------------------------------------- using System; using System.Data; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Data.DataSetExtensions; namespace System.Data { ////// This class implements IEqualityComparer using value based semantics /// when comparing DataRows. /// public static class DataRowComparer { ////// Gets the singleton instance of the data row comparer. /// public static DataRowComparerDefault { get { return DataRowComparer .Default; } } internal static bool AreEqual(object a, object b) { if (Object.ReferenceEquals(a, b)) { // same reference or (null, null) or (DBNull.Value, DBNull.Value) return true; } if (Object.ReferenceEquals(a, null) || Object.ReferenceEquals(a, DBNull.Value) || Object.ReferenceEquals(b, null) || Object.ReferenceEquals(b, DBNull.Value)) { // (null, non-null) or (null, DBNull.Value) or vice versa return false; } return (a.Equals(b) || (a.GetType().IsArray && CompareArray((Array)a, b as Array))); } private static bool AreElementEqual(object a, object b) { if (Object.ReferenceEquals(a, b)) { // same reference or (null, null) or (DBNull.Value, DBNull.Value) return true; } if (Object.ReferenceEquals(a, null) || Object.ReferenceEquals(a, DBNull.Value) || Object.ReferenceEquals(b, null) || Object.ReferenceEquals(b, DBNull.Value)) { // (null, non-null) or (null, DBNull.Value) or vice versa return false; } return a.Equals(b); } private static bool CompareArray(Array a, Array b) { if ((null == b) || (1 != a.Rank) || (1 != b.Rank) || (a.Length != b.Length)) { // automatically consider array's with Rank>1 not-equal return false; } int index1 = a.GetLowerBound(0); int index2 = b.GetLowerBound(0); if (a.GetType() == b.GetType() && (0 == index1) && (0 == index2)) { switch (Type.GetTypeCode(a.GetType().GetElementType())) { case TypeCode.Byte: return DataRowComparer.CompareEquatableArray ((Byte[])a, (Byte[])b); case TypeCode.Int16: return DataRowComparer.CompareEquatableArray ((Int16[])a, (Int16[])b); case TypeCode.Int32: return DataRowComparer.CompareEquatableArray ((Int32[])a, (Int32[])b); case TypeCode.Int64: return DataRowComparer.CompareEquatableArray ((Int64[])a, (Int64[])b); case TypeCode.String: return DataRowComparer.CompareEquatableArray ((String[])a, (String[])b); } } //Compare every element. But don't recurse if we have Array of array. int length = index1 + a.Length; for (; index1 < length; ++index1, ++index2) { if (!AreElementEqual(a.GetValue(index1), b.GetValue(index2))) { return false; } } return true; } private static bool CompareEquatableArray (TElem[] a, TElem[] b) where TElem : IEquatable { if (Object.ReferenceEquals(a, b)) { return true; } if (Object.ReferenceEquals(a, null) || Object.ReferenceEquals(b, null)) { return false; } if (a.Length != b.Length) { return false; } for (int i = 0; i < a.Length; ++i) { if (!a[i].Equals(b[i])) { return false; } } return true; } } /// /// This class implements IEqualityComparer using value based semantics /// when comparing DataRows. /// public sealed class DataRowComparer: IEqualityComparer where TRow : DataRow { /// /// Private constructor to prevent initialization outside of Default singleton instance. /// private DataRowComparer() { } private static DataRowComparer_instance = new DataRowComparer (); /// /// Gets the singleton instance of the data row comparer. /// public static DataRowComparerDefault { get { return _instance; } } /// /// This method compares to DataRows by doing a column by column value based /// comparision. /// /// /// The first input DataRow /// /// /// The second input DataRow /// ////// True if rows are equal, false if not. /// public bool Equals(TRow leftRow, TRow rightRow) { if (Object.ReferenceEquals(leftRow, rightRow)) { return true; } if (Object.ReferenceEquals(leftRow, null) || Object.ReferenceEquals(rightRow, null)) { return false; } if (leftRow.RowState == DataRowState.Deleted || rightRow.RowState == DataRowState.Deleted) { throw DataSetUtil.InvalidOperation(Strings.DataSetLinq_CannotCompareDeletedRow); } int count = leftRow.Table.Columns.Count; if (count != rightRow.Table.Columns.Count) { return false; } for (int i = 0; i < count; ++i) { if (!DataRowComparer.AreEqual(leftRow[i], rightRow[i])) { return false; } } return true; } ////// This mtheod retrieves a hash code for the source row. /// /// /// The source DataRow /// ////// HashCode for row based on values in the row. /// public int GetHashCode(TRow row) { DataSetUtil.CheckArgumentNull(row, "row"); if (row.RowState == DataRowState.Deleted) { throw DataSetUtil.InvalidOperation(Strings.DataSetLinq_CannotCompareDeletedRow); } int hash = 0; Debug.Assert(row.Table != null); if (row.Table.Columns.Count > 0) { // if the row has at least one column, then use the first column value object value = row[0]; Type valueType = value.GetType(); if (valueType.IsArray) { Array array = value as Array; if (array.Rank > 1) { hash = value.GetHashCode(); } else if (array.Length > 0) { hash = array.GetValue(array.GetLowerBound(0)).GetHashCode(); } } else { System.ValueType vt = value as System.ValueType; // have to unbox value types. if (vt != null) { hash = vt.GetHashCode(); } else { hash = value.GetHashCode(); } } } // if table has no columns, the hash code is 0 return hash; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //[....] //spather //----------------------------------------------------------------------------- using System; using System.Data; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Data.DataSetExtensions; namespace System.Data { ////// This class implements IEqualityComparer using value based semantics /// when comparing DataRows. /// public static class DataRowComparer { ////// Gets the singleton instance of the data row comparer. /// public static DataRowComparerDefault { get { return DataRowComparer .Default; } } internal static bool AreEqual(object a, object b) { if (Object.ReferenceEquals(a, b)) { // same reference or (null, null) or (DBNull.Value, DBNull.Value) return true; } if (Object.ReferenceEquals(a, null) || Object.ReferenceEquals(a, DBNull.Value) || Object.ReferenceEquals(b, null) || Object.ReferenceEquals(b, DBNull.Value)) { // (null, non-null) or (null, DBNull.Value) or vice versa return false; } return (a.Equals(b) || (a.GetType().IsArray && CompareArray((Array)a, b as Array))); } private static bool AreElementEqual(object a, object b) { if (Object.ReferenceEquals(a, b)) { // same reference or (null, null) or (DBNull.Value, DBNull.Value) return true; } if (Object.ReferenceEquals(a, null) || Object.ReferenceEquals(a, DBNull.Value) || Object.ReferenceEquals(b, null) || Object.ReferenceEquals(b, DBNull.Value)) { // (null, non-null) or (null, DBNull.Value) or vice versa return false; } return a.Equals(b); } private static bool CompareArray(Array a, Array b) { if ((null == b) || (1 != a.Rank) || (1 != b.Rank) || (a.Length != b.Length)) { // automatically consider array's with Rank>1 not-equal return false; } int index1 = a.GetLowerBound(0); int index2 = b.GetLowerBound(0); if (a.GetType() == b.GetType() && (0 == index1) && (0 == index2)) { switch (Type.GetTypeCode(a.GetType().GetElementType())) { case TypeCode.Byte: return DataRowComparer.CompareEquatableArray ((Byte[])a, (Byte[])b); case TypeCode.Int16: return DataRowComparer.CompareEquatableArray ((Int16[])a, (Int16[])b); case TypeCode.Int32: return DataRowComparer.CompareEquatableArray ((Int32[])a, (Int32[])b); case TypeCode.Int64: return DataRowComparer.CompareEquatableArray ((Int64[])a, (Int64[])b); case TypeCode.String: return DataRowComparer.CompareEquatableArray ((String[])a, (String[])b); } } //Compare every element. But don't recurse if we have Array of array. int length = index1 + a.Length; for (; index1 < length; ++index1, ++index2) { if (!AreElementEqual(a.GetValue(index1), b.GetValue(index2))) { return false; } } return true; } private static bool CompareEquatableArray (TElem[] a, TElem[] b) where TElem : IEquatable { if (Object.ReferenceEquals(a, b)) { return true; } if (Object.ReferenceEquals(a, null) || Object.ReferenceEquals(b, null)) { return false; } if (a.Length != b.Length) { return false; } for (int i = 0; i < a.Length; ++i) { if (!a[i].Equals(b[i])) { return false; } } return true; } } /// /// This class implements IEqualityComparer using value based semantics /// when comparing DataRows. /// public sealed class DataRowComparer: IEqualityComparer where TRow : DataRow { /// /// Private constructor to prevent initialization outside of Default singleton instance. /// private DataRowComparer() { } private static DataRowComparer_instance = new DataRowComparer (); /// /// Gets the singleton instance of the data row comparer. /// public static DataRowComparerDefault { get { return _instance; } } /// /// This method compares to DataRows by doing a column by column value based /// comparision. /// /// /// The first input DataRow /// /// /// The second input DataRow /// ////// True if rows are equal, false if not. /// public bool Equals(TRow leftRow, TRow rightRow) { if (Object.ReferenceEquals(leftRow, rightRow)) { return true; } if (Object.ReferenceEquals(leftRow, null) || Object.ReferenceEquals(rightRow, null)) { return false; } if (leftRow.RowState == DataRowState.Deleted || rightRow.RowState == DataRowState.Deleted) { throw DataSetUtil.InvalidOperation(Strings.DataSetLinq_CannotCompareDeletedRow); } int count = leftRow.Table.Columns.Count; if (count != rightRow.Table.Columns.Count) { return false; } for (int i = 0; i < count; ++i) { if (!DataRowComparer.AreEqual(leftRow[i], rightRow[i])) { return false; } } return true; } ////// This mtheod retrieves a hash code for the source row. /// /// /// The source DataRow /// ////// HashCode for row based on values in the row. /// public int GetHashCode(TRow row) { DataSetUtil.CheckArgumentNull(row, "row"); if (row.RowState == DataRowState.Deleted) { throw DataSetUtil.InvalidOperation(Strings.DataSetLinq_CannotCompareDeletedRow); } int hash = 0; Debug.Assert(row.Table != null); if (row.Table.Columns.Count > 0) { // if the row has at least one column, then use the first column value object value = row[0]; Type valueType = value.GetType(); if (valueType.IsArray) { Array array = value as Array; if (array.Rank > 1) { hash = value.GetHashCode(); } else if (array.Length > 0) { hash = array.GetValue(array.GetLowerBound(0)).GetHashCode(); } } else { System.ValueType vt = value as System.ValueType; // have to unbox value types. if (vt != null) { hash = vt.GetHashCode(); } else { hash = value.GetHashCode(); } } } // if table has no columns, the hash code is 0 return hash; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- KeySpline.cs
- Debug.cs
- RegexFCD.cs
- XmlNamespaceManager.cs
- DTCTransactionManager.cs
- ThrowHelper.cs
- XmlCharCheckingReader.cs
- Condition.cs
- QueryExpr.cs
- ProtocolsSection.cs
- VoiceObjectToken.cs
- OutputCacheProfileCollection.cs
- TabControl.cs
- XmlSerializableServices.cs
- GraphicsContainer.cs
- Profiler.cs
- ListViewUpdateEventArgs.cs
- TailPinnedEventArgs.cs
- NGCPageContentSerializerAsync.cs
- Int32Collection.cs
- DataGridViewLayoutData.cs
- ExternalException.cs
- Attribute.cs
- UrlParameterReader.cs
- ApplicationDirectory.cs
- WebUtil.cs
- streamingZipPartStream.cs
- DbgUtil.cs
- wgx_exports.cs
- ToggleButton.cs
- SplitterEvent.cs
- OdbcConnectionString.cs
- XhtmlBasicValidatorAdapter.cs
- WindowsIPAddress.cs
- OdbcRowUpdatingEvent.cs
- IndexOutOfRangeException.cs
- Contracts.cs
- DataGridTableCollection.cs
- MethodBuilder.cs
- CommandEventArgs.cs
- BamlLocalizabilityResolver.cs
- ProxyWebPart.cs
- _LocalDataStore.cs
- CodeTypeConstructor.cs
- DnsCache.cs
- Size3DConverter.cs
- MappedMetaModel.cs
- SplashScreen.cs
- PtsHost.cs
- RelationshipEndCollection.cs
- _IPv4Address.cs
- ResourcesBuildProvider.cs
- XmlIlGenerator.cs
- DataTemplateSelector.cs
- DataBindingList.cs
- PersonalizationProviderCollection.cs
- PersonalizationStateInfo.cs
- GridErrorDlg.cs
- ContentDefinition.cs
- QilTernary.cs
- XmlQueryRuntime.cs
- FloaterBaseParaClient.cs
- CanonicalizationDriver.cs
- ConstNode.cs
- HuffModule.cs
- DynamicValueConverter.cs
- DataGridTable.cs
- OutputCacheSection.cs
- DataSourceSelectArguments.cs
- XPathDocumentBuilder.cs
- DesignerActionKeyboardBehavior.cs
- Shared.cs
- Context.cs
- EventSetter.cs
- XmlMembersMapping.cs
- XmlText.cs
- QilValidationVisitor.cs
- RuleSettings.cs
- UpWmlMobileTextWriter.cs
- Win32PrintDialog.cs
- PersonalizationDictionary.cs
- ParameterReplacerVisitor.cs
- LabelAutomationPeer.cs
- RectangleGeometry.cs
- WebPartMinimizeVerb.cs
- Base64Encoder.cs
- UnmanagedBitmapWrapper.cs
- TextBoxAutomationPeer.cs
- TreeNodeBinding.cs
- ImageCodecInfo.cs
- GlobalAllocSafeHandle.cs
- GenericWebPart.cs
- ClientUrlResolverWrapper.cs
- GlobalDataBindingHandler.cs
- PersistenceContextEnlistment.cs
- LexicalChunk.cs
- ObjectDataSourceMethodEventArgs.cs
- GeometryModel3D.cs
- ComponentResourceKeyConverter.cs
- MatrixValueSerializer.cs