Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6604457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:09:31+00:00 2026-05-25T19:09:31+00:00

I have 2 lists List<Class1> and List<Class2> that are compared by same property Class1.Key

  • 0

I have 2 lists List<Class1> and List<Class2> that are compared by same property Class1.Key and Class2.Key (string) and I want to write a function that will produce 3 lists out of them

  1. List<Class1> Elements that are present in both lists
  2. List<Class1> Elements that are present only in first list
  3. List<Class2> Elements that are present only in second list

Is there a quick way to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:09:32+00:00Added an answer on May 25, 2026 at 7:09 pm
    var requirement1 = list1.Intersect(list2);
    var requirement2 = list1.Except(list2);
    var requirement3 = list2.Except(list1);
    

    For your List<string>, this will be all you need. If you were doing this for a custom class and you were looking for something other than reference comparisons, you’d want to ensure that the class properly overrided Equals and GetHashCode. Alternatively, you could provide an IEqualityComparer<YourType> to overloads of the above methods.

    Edit:

    OK, now you’ve indicated in the comments that it isn’t a list of string, it’s a List<MyObject>. In which case, override Equals/GetHashCode (if your key should uniquely identify these classes all the time and you have access to the source code) or provide an IEqualityComparer implementation (still involves Equals/GetHashCode, use this if the comparison is unique to these requires or if you do not have access to MyObject source).

    For example:

    class MyObjectComparer : IEqualityComparer<MyObject>
    {
         public bool Equals(MyObject x, MyObject y)
         {
              // implement appropriate comparison of x and y, check for nulls, etc 
         }
    
         public int GetHashCode(MyObject obj)
         {
              // validate if necessary
              return obj.KeyProperty.GetHashCode();
         }
    }
    

    If you used a custom equality comparer such as this, the call to the above methods would be

    list1.Intersect(list2, customComparerInstance);
    

    Edit: And now you’ve moved the bar yet again, this time the problem deals with two distinct classes. For this, you would consider utilizing join operations, one being an inner, the other being an outer.

    In the case of

    class Class1
    {
        public string Foo { get; set; } 
    }
    
    class Class2
    {
        public string Bar { get; set; }
    }
    

    You could write

    var intersect = from item1 in list1
                    join item2 in list2
                    on item1.Foo equals item2.Bar
                    select item1;
    
    var except1 = from item1 in list1
                    join item2 in list2
                    on item1.Foo equals item2.Bar into gj
                    from item2 in gj.DefaultIfEmpty()
                    where item2 == null
                    select item1;
    

    To get the items in list2 without matching* objects in list1, simply reverse the order of the lists/items in the except1 query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So let's assume I have a class named ABC that will have a list
I have a bunch of lists ( List<SomeClass> ) and I want to get
I have a list of class items List<MyClass> I have a seperate object that
I have a ViewModel class that contains a list of points, and I am
I have a class that wraps List<> I have GetValue by index method: public
I have a class that contains a list of objects. What's the best way
I have a class that includes a std::list and wish to provide public begin()
I have a class that has an list<Book> in it, and those Book objects
I have a class that has an list<Book> in it, and those Book objects
I have an initialization class that preloads content into a variable (probably a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.