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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:50:44+00:00 2026-06-05T15:50:44+00:00

I have a class, called ClassX for the purposes of this question, declared as

  • 0

I have a class, called ClassX for the purposes of this question, declared as follows:

class X 
{ 
    public Guid Id { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
}

If I have two instances (instanceOne and instanceTwo) of List<T> of these classes, how can I find the elements that are the same in both instances:

Assume that there is two elements in instanceOne and four elements in instanceTwo. Two of the elements are the same (as defined by the fact that they have the same Guid Id) in each of the instances

I thought I should be able to LINQ my way to it but this isn’t doing it for me

// What's common to the two instances?
var commonElements = (
    from a in instanceTwo
    join b in instanceOne on a.Id equals b.Id
    select b).ToList();

// What's not in instanceOne and in instanceTwo? 
var notInInstanceOne = instanceTwo.Except(commonElements)

In this situation instanceTwo is a superset of instanceOne but this may not always be the case so I should be able to flip the original LINQ statement to get the elements in instanceOne that are not in instanceTwo viz:

var notInInstanceTwo = instanceOne.Except(commonElements)

Frustratingly the original LINQ statement (where I attempt to determine the common elements) isn’t working, can anyone spot what I’m doing wrong?

EDIT 2012-06-08 11:00 UTC

Per @Nikhil Agrawal and @Trust me – I’m a Doctor I have used the Intersect method but this doesn’t produce the expected results:

    var commonItems = instanceTwo.Intersect(instanceOne); // Returns nothing
    var itemsInTwoNotOne= instanceTwo.Except(instanceOne); // Returns everything in instanceTwo

FWIW my implementation of the Equals() method is:

public bool Equals(Guid x, Guid y)
{
   if (x == y)
   {
      return true;
   }
   return false;
}
  • 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-05T15:50:46+00:00Added an answer on June 5, 2026 at 3:50 pm

    Use can use intersect to get a list of all common elements. You should also create a IEqualityComparer, since you want to identify the elements by its Id.

    Example:

    class Program
    {
        class X
        {
            public Guid Id { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
        }
    
        class XequalityComparer : IEqualityComparer<X>
        {
            //Note: maybe add a null check in these methods
            public bool Equals(X x, X y) { return x.Id.Equals(y.Id); }
            public int GetHashCode(X obj) { return obj.Id.GetHashCode(); }
        }
    
        static void Main(string[] args)
        {
            var instanceOne = new List<X>() { 
                new X() { Id = Guid.NewGuid() }, 
                new X() { Id = Guid.Parse("EF42EE32-1B9E-493C-9D39-4610E0FB29D0") } 
            };
            var instanceTwo = new List<X>() { 
                new X() { Id = Guid.NewGuid() }, 
                new X() { Id = Guid.Parse("EF42EE32-1B9E-493C-9D39-4610E0FB29D0") } 
            };
    
            var common = instanceOne.Intersect(instanceTwo, new XequalityComparer());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class called: ComplexNumber and I have a string that I need
I have a class called Account public class Account { public double balance =
I have a class called Metadata, which is declared within the namespace A::B::C ,
I have a class called Property which has nothing but get -methods. All the
I have the following extension method: public static void ThrowIfArgumentIsNull<T>(this T value, string argument)
I have a class called LayoutManager. The purpose of this class is to hold
I have this class, just for the purpose of learning: public class MyClass{ //Looking
Suppose that we have a class called class1. The class1 has a method called
I have a class called Trial which has_many results. Now What I want to
I have a class called Flamethrower which naturally has its own ammunition that is

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.