Compare two different Lists

Hi ,

I have the following two classes with properties

Class ABC
{
 Property int code{get;set;}
Property string name {get;set;}
}
Class DEF
{
Property int code{get;set;}
Property string name {get;set;}
}

Now I have List< ABC> and List < DEF>
And I want to compare these two lists and get the values of DEF which is not present in ABC, I have used following code to achieve that
var matchedAccounts =
                    DEF.Where(x => ABC.Any(y => y.Code == x. Code)).ToList();

                var unMatchedAccounts = DEF.Except(matchedAccounts).ToList();

Is this the right way , or will have any impact on performance, if so what is the right way , please guide me.
praveen1981Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rafiq J.Net DeveloperCommented:
Use Except to compare the two list: Think abc is list1 & cde is the List.

var firstNotSecond = list1.Except(list2).ToList();
var secondNotFirst = list2.Except(list1).ToList();

It works faster than

var list1 = list.Where(i => !list2.Contains(i)).ToList();
var list2 = list2.Where(i => !list.Contains(i)).ToList();



Like if it's works.
any doubt post your comments
0
Michael FowlerSolutions ConsultantCommented:
To ensure that the objects compare correctly using either of the methods discussed you should also implement the IEquatable<T> generic interface.

Class ABC  : IEquatable<ABC>
{
 Property int code{get;set;}
Property string name {get;set;}

    public bool Equals(Product other)
    {
        if (Object.ReferenceEquals(other, null)) return false;
        if (Object.ReferenceEquals(this, other)) return true;
        return Code.Equals(other.Code) && Name.Equals(other.Name);
    }

    public override int GetHashCode()
    {
        int hashProductName = Name == null ? 0 : Name.GetHashCode();
        int hashProductCode = Code.GetHashCode();
        return hashProductName ^ hashProductCode;
    }
}

Open in new window

https://msdn.microsoft.com/en-us/library/vstudio/bb300779(v=vs.100).aspx
0
praveen1981Author Commented:
Hi Rafiq J,

I am talking about two different types(class objects), but it seems the answer which you have provided will work for the same type of objects in list
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

praveen1981Author Commented:
Hi Michael74

Is there any way in linq to achieve my task.
0
Michael FowlerSolutions ConsultantCommented:
Hi Praveen1981

You can still use Linq with the answer provided by RafiqJ you just need to ensure that the objects will be considered equivalent when the properties are equal.

Each object regardless of its properties is normally considered a separate entity and therefore not equivalent to any other object. The method I posted above overrides this default behaviour and causes the objects to be considered as equal when the productName and code fields are equal in both objects. Once you have done this then the Linq expressions will work as required
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
praveen1981Author Commented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.