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

Determine if there are deltas/diffs Between Two Lists Of Same DataType


// Determine if there are unsaved changes
 var changedItems = from item1 in BoundJHAStaleNotifications
 from item2 in JHAStaleNotifications
 where item1.JHAStatus == item2.JHAStatus
 && item1.StaleUserType == item2.StaleUserType
 && ItemsAreDifferent(item1, item2)
 select item1;

 if (changedItems.Count() > 0)
 {
 ((EHAP_MasterPage)Master).ErrorMessage = "There are unsaved changes on this tab. Please either Save or Cancel before proceeding.";
 return;
 }


private bool ItemsAreDifferent(JHAStaleNotification item1, JHAStaleNotification item2)
 {

 bool ItemsAreDifferent = (item1.JHAStatus == item2.JHAStatus &&
 item1.StaleUserType == item2.StaleUserType &&
 item1.EntityStateType == item2.EntityStateType &&
 item1.Frequency == item2.Frequency &&
 item1.SendNotification == item2.SendNotification &&
 item1.Threshold == item2.Threshold);

 return !ItemsAreDifferent;

 }

How To Easily Compare 2 Lists To Determine Differences

This will work with complex types too.

bool changesMade = ListOne.SequenceEqual(ListTwo);

Note this uses the default equality compare (Equals). You may need to override the equals.

 public override bool Equals(object obj)
        {
            if (obj is JHAStaleNotification)
            {
                JHAStaleNotification other = (JHAStaleNotification)obj;
                return (this.JHAStatus == other.JHAStatus && 
                    this.StaleUserType == other.StaleUserType &&
                    this.EntityStateType == other.EntityStateType &&
                    this.Frequency == other.Frequency &&
                    this.SendNotification == other.SendNotification &&
                    this.Threshold == other.Threshold);
            }
            else
                return false;
        }

Sorting Lists With Linq and Lamdas

Function to Sort List by “Text” property using Linq

        private List<TVEntity> sortTVObject(List<TVEntity> tvs)
        {
            var sortedTVs =
       from t in tvs
       orderby t.Text
       select t;

            return sortedTVs.ToList();
        }

TVEntity.cs (Object)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.dereksandbox.bl.Entities
{
    [Serializable]
    public class TVEntity
    {
        public string Text { get; set; }
        public string Value { get; set; }
    }
}

Execution

            List<TVEntity> TVs = new List<TVEntity>();

            // create some dummy data
            TVs.Add(new TVEntity() { Text = "Samsung", Value = "1" });
            TVs.Add(new TVEntity() { Text = "LG", Value = "2" });
            TVs.Add(new TVEntity() { Text = "Song", Value = "3" });
            TVs.Add(new TVEntity() { Text = "Apex", Value = "4" });
            TVs.Add(new TVEntity() { Text = "Olevia", Value = "5" });
            TVs.Add(new TVEntity() { Text = "Mitshibi", Value = "6" });
            TVs.Add(new TVEntity() { Text = "Sharp", Value = "7" });

            SortedAvailableChoices = sortTVObject(TVs);

You could also use Lamda’s to perform the sorting as well:


List<TVEntity> TVs = new List<TVEntity>();

// create some dummy data
TVs.Add(new TVEntity() { Text = "Samsung", Value = "1" });
TVs.Add(new TVEntity() { Text = "LG", Value = "2" });
TVs.Add(new TVEntity() { Text = "Song", Value = "3" });
TVs.Add(new TVEntity() { Text = "Apex", Value = "4" });
TVs.Add(new TVEntity() { Text = "Olevia", Value = "5" });
TVs.Add(new TVEntity() { Text = "Mitshibi", Value = "6" });
TVs.Add(new TVEntity() { Text = "Sharp", Value = "7" });           

// sort using a Lamda expression
TVs.Sort((x, y) => (x.Text.CompareTo(y.Text)));