Question Get updated by comparing two List<Object>

PD1991

Member
Joined
Nov 17, 2021
Messages
18
Programming Experience
1-3
Hello,

I have two list of object and I want to get added, removed and updated by comparing two List<Object>.
I have managed to get removed and added but I am not able to find out how I can get updated by comparing two List<Object>.
Eg.
oldValue = {
Name : Q1,
DataType: Bool,
IsActive: True
}
newValue = {
Name : Q1,
DataType: Bool,
IsActive: False
}

Than in updated list I want
newValue = {
Name : Q1,
DataType: Bool,
IsActive: False
}

Below is the code I am using for added and removed.
C#:
var removed = oldValue.Except(newValue, new AssignUserViewModelEqualityComparer()).ToList();
var added = newValue.Except(oldValue, new AssignUserViewModelEqualityComparer()).ToList();

  class AssignUserViewModelEqualityComparer : IEqualityComparer<Qualifier>
  {
    public bool Equals(Qualifier x, Qualifier y)
    {
      if (object.ReferenceEquals(x, y))
        return true;
      if (x == null || y == null)
        return false;
      return x.Name.Equals(y.Name);
    }

    public int GetHashCode(Qualifier obj)
    {
      return obj.Name.GetHashCode();
    }
  }
      public class Qualifier
    {

        [XmlElement(ElementName = "name")]
        public string Name { get; set; }

        [XmlElement(ElementName = "dataType")]
        public string DataType { get; set; }

        [XmlElement(ElementName = "isActive")]
        public bool IsActive { get; set; }
    }
 
Don't you mean that you have two List<Qualifier> rather than two List<Object> ?

Anyway, it's unclear what you mean by "updated". How did you determine that it was newValue is the result that you wanted, rather than oldValue?
 
Don't you mean that you have two List<Qualifier> rather than two List<Object> ? Its two List<Qualifier>.
First List<Qualifier> is named as oldValue and second List<Qualifier> is named as newValue.
If the Name in both List<Qualifier> are same and apart from Name other properties like datatype or IsActive has been changed than in that case its Updated.
 
Don't you mean that you have two List<Qualifier> rather than two List<Object> ? Its two List<Qualifier>.
First List<Qualifier> is named as oldValue and second List<Qualifier> is named as newValue.
If the Name in both List<Qualifier> are same and apart from Name other properties like datatype or IsActive has been changed than in that case its Updated.
For more clarity... I assume this is for two lists of the same type.
Typing parameters with Object; allows for the specification of two Lists which could share no type equality; except for their shared inheritance of the base class Object. Parametrically using Object as opposed to a common Generic type and / or fixed type is generally not useful in this context; because at the Object type casting level; none of the specific type attributes of List 1 and 2 would be accessible.
 
This is untested but I think it should work:
C#:
var qualifierEqualityComparer = new AssignUserViewModelEqualityComparer();
var removed = oldValue.Except(newValue, qualifierEqualityComparer).ToList();
var added = newValue.Except(oldValue, qualifierEqualityComparer).ToList();
var updated = oldValue.Where(qo => newValue.Any(qn => qualifierEqualityComparer.Equals(qo, qn) && (qo.DataType != qn.DataType || qo.IsActive != qn.IsActive))).ToList();
 
Back
Top Bottom