How To Compare 2 Lists Determining If A Property Has Changed, Ignoring Order

In the list you want to perform the comparison against, override Equals

 [Serializable]
    public class AlertSubscriptionOption
    {
..
.
  /// <summary>
        /// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
        /// <returns>
        ///   <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj)
        {
            // Check for null values and compare run-time types.
            if (obj == null || GetType() != obj.GetType())
                return false;

            AlertSubscriptionOption p = (AlertSubscriptionOption)obj;
            return this.SubType == p.SubType;
        }

Now, to compare the 2 lists, perform the following


   
     bool subscriptionEmailDifferent = false;
   
            
    subscriptionEmailDifferent = (newSubscriptionEmailOptions.Count > 0 || oldSubscriptionEmailOptions.Count > 0) &&
                                          !newSubscriptionEmailOptions.OrderBy(x => x.SubType).ToList().SequenceEqual(oldSubscriptionEmailOptions.OrderBy(x => x.SubType).ToList());

References:

http://stackoverflow.com/questions/12795882/quickest-way-to-compare-two-list
http://stackoverflow.com/questions/188141/list-orderby-alphabetical-order

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s