Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

c# - Get the differences between 2 lists

I have two lists (ListA and ListB), the type of these lists is the same PersonInfo, the Loginfield is a unique key.

public class PersonInfo
{
    public string Login { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public bool Active { get; set; }
}

I'd like compare these two lists :

  1. I'd like get a list of items in ListA not available in ListB

  2. For the items available in both lists, I'd like get a list from ListA (Login field is a unique key) for the item where there is a difference between the two lists.

Example : if for the Login "MyLogin" in ListA, the value of FirstName does not match with the value in ListB. The item with "MyLogin" as Login must be part of the result list.

Example : if the Age between ListA and ListB for a specific login is different, the item must be part of the result

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To compare objects of custom data type lists, you will need to implement IEquatable in your class and override GetHashCode()

Check this MSDN Link

Your class

    public class PersonInfo : IEquatable<PersonInfo>
    {
        public string Login { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public bool Active { get; set; }

        public bool Equals(PersonInfo other)
        {
            //Check whether the compared object is null.
            if (Object.ReferenceEquals(other, null)) return false;

            //Check whether the compared object references the same data.
            if (Object.ReferenceEquals(this, other)) return true;

            //Check whether the properties are equal.
            return Login.Equals(other.Login) && FirstName.Equals(other.FirstName) && LastName.Equals(other.LastName) && Age.Equals(other.Age) && Active.Equals(other.Active);
        }

        public override int GetHashCode()
        {

            int hashLogin = Login == null ? 0 : Login.GetHashCode();

            int hashFirstName = FirstName == null ? 0 : FirstName.GetHashCode();

            int hashLastName = LastName == null ? 0 : LastName.GetHashCode();

            int hashAge = Age.GetHashCode();

            int hashActive = Active.GetHashCode();

            //Calculate the hash code.
            return (hashLogin + hashFirstName + hashLastName) ^ (hashAge + hashActive);
        }
    }

Then here is how you use it (as listed in Pranay's response)

            List<PersonInfo> ListA = new List<PersonInfo>() { new PersonInfo { Login = "1", FirstName = "James", LastName = "Watson", Active = true, Age = 21 }, new PersonInfo { Login = "2", FirstName = "Jane", LastName = "Morrison", Active = true, Age = 25 }, new PersonInfo { Login = "3", FirstName = "Kim", LastName = "John", Active = false, Age = 33 } };
            List<PersonInfo> ListB = new List<PersonInfo>() { new PersonInfo { Login = "1", FirstName = "James2222", LastName = "Watson", Active = true, Age = 21 }, new PersonInfo { Login = "3", FirstName = "Kim", LastName = "John", Active = false, Age = 33 } };

            //Get Items in ListA that are not in ListB
            List<PersonInfo> FilteredListA = ListA.Except(ListB).ToList();

            //To get the difference between ListA and FilteredListA (items from FilteredListA will be removed from ListA)
            ListA.RemoveAll(a => FilteredListA.Contains(a));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...