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 7951777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:39:50+00:00 2026-06-04T02:39:50+00:00

If I have two generic lists. How do I find what’s NOT in the

  • 0

If I have two generic lists. How do I find what’s NOT in the other list using LINQ?

Here is my obj class:

 public class tobj
 {      
    public string column1;
    public string column2;
 }  

Here are the arrays

 tobj[] array1 = 
 { 
        new tobj { "1", "1" }, new tobj { "2", "2" }, new tobj { "3", "3" } 
 };

 tobj[] array2 = 
 { 
     new tobj { "2", "2" }, new tobj { "3", "3" }, new tobj { "4", "4" } 
 };

 var diff = array1.FinfDiff(array2); 

 foreach (var value in diff)
 {
    Console.WriteLine(value.column1); // 1
 }

This code not tested yet.

  • 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-06-04T02:39:51+00:00Added an answer on June 4, 2026 at 2:39 am

    You can use Except. Given that you have a custom type, though, you’ll want to override Equals() and GetHashCode() so that it works well with Linq.

    This is because Linq strives for efficiency, so it can use HashSet<T> and other mechanisms to quickly determine if items are possibly equivalent or not (items that are equivalent must have same hash code, or your hash code is bad). So it’s not just enough to implement Equals(), you must also implement GetHashCode() when using methods of this type with custom classes.

    For example:

        public class tobj : IEquatable<tobj>
        {
            public string column1;
            public string column2;
    
            public bool Equals(tobj other)
            {
                return other != null ? Equals(column1, other.column1) && Equals(column2, other.column2) : false;
            }
    
            public override bool Equals(object obj)
            {
                return Equals(obj as tobj);
            }
    
            public override int GetHashCode()
            {
                // seed the code
                int hash = 13;
    
                // use some primes to mix in the field hash codes...
                hash = hash * 17 + column1.GetHashCode();
                hash = hash * 17 + column2.GetHashCode();
    
                return hash
            }
        }  
    

    Now that you have GetHashCode() and Equals() overridden (I like implementing IEquatable<T> myself as well), you can use Except():

            var results = array1.Except(array2);
    
            foreach (var i in results)
            {
                Console.WriteLine(i.column1 + ":" + i.column2);
            }
    

    I’d recommend seeing this SO thread on algorithms for hash codes as well for tips on implementing a good one.

    Also, as a side note, some types (such as KeyValuePair, Tuple, and anonymous types) already implement Equals() and GetHashCode() correctly for equivalence in such a way that they take into account their individual components.

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

Sidebar

Related Questions

I have two Generic lists having objects of class : class Subject { public
I have a generic list which I form from two other lists. The orderby
I have two generic lists. Let's say they are List< A > and List<
I have two generic lists where I want to run a couple of Linq
I have two threads, a producer thread that places objects into a generic List
I have two classes: Superclass and derived Subclass:Superclass. I have a generic method: public
I have two EmailAddress generic lists, I wanted a simple way of just getting
I have two generic list objects, in which one contains ids and ordering, and
Let's say I have two generic lists of the same type. How do I
I'll try to clarify by using the following example. In here I have two

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.