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

  • Home
  • SEARCH
  • 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 9216175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:21:40+00:00 2026-06-18T02:21:40+00:00

The solutions I have found so far in my research on comparing lists of

  • 0

The solutions I have found so far in my research on comparing lists of objects have usually generated a new list of objects, say of those items existing in one list, but not in the other. In my case, I want to compare two lists to discover the items whose key exists in one list and not the other (comparing both ways), and for those keys found in both lists, checking whether the value is the same or different.

The object being compared has multiple properites that constitute the key, plus a property that constitutes the value, and finally, an enum property that describes the result of the comparison, e.g., {Equal, NotEqual, NoMatch, NotYetCompared}. So my object might look like:

class MyObject
{
   //Key combination
   string columnA;
   string columnB;
   decimal columnC;

   //The Value
   decimal columnD;

   //Enum for comparison, used for styling the item (value hidden from UI)
   //Alternatively...this could be a string type, holding the enum.ToString()
   MyComparisonEnum result;
}

These objects are collected into two ObservableCollection<MyObject> to be compared. When bound to the UI, the grid rows are being styled based on the caomparison result enum, so the user can easily see what keys are in the new dataset but not in the old, vice-versa, along with those keys in both datasets with a different value. Both lists are presented in the UI in data grids, with the rows styled based on the comparison result.

Would LINQ be suitable as a tool to solve this efficiently, or should I use loops to scan the lists and break out when the key is found, etc (a solution like this comes naturally to be from my procedural programming background)… or some other method?

Thank you!

  • 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-18T02:21:41+00:00Added an answer on June 18, 2026 at 2:21 am

    You can use Except and Intersect:

    var list1 = new List<MyObject>();
    var list2 = new List<MyObject>();
    // initialization code 
    var notIn2 = list1.Except(list2);
    var notIn1 = list2.Except(list1);
    var both = list1.Intersect(list2);
    

    To find objects with different values (ColumnD) you can use this (quite efficient) Linq query:

    var diffValue = from o1 in list1
                    join o2 in list2
                    on new { o1.columnA, o1.columnB, o1.columnC } equals new { o2.columnA, o2.columnB, o2.columnC }
                    where o1.columnD != o2.columnD
                    select new { Object1 = o1, Object2 = o2 };
    
    foreach (var diff in diffValue)
    {
        MyObject obj1 = diff.Object1;
        MyObject obj2 = diff.Object2;
        Console.WriteLine("Obj1-Value:{0} Obj2-Value:{1}", obj1.columnD, obj2.columnD);
    }
    

    when you override Equals and GetHashCode appropriately:

    class MyObject
    {
        //Key combination
        string columnA;
        string columnB;
        decimal columnC;
    
        //The Value
        decimal columnD;
    
        //Enum for comparison, used for styling the item (value hidden from UI)
        //Alternatively...this could be a string type, holding the enum.ToString()
        MyComparisonEnum result;
    
        public override bool Equals(object obj)
        {
            if (obj == null || !(obj is MyObject)) return false;
            MyObject other = (MyObject)obj;
            return columnA.Equals(other.columnA) && columnB.Equals(other.columnB) && columnC.Equals(other.columnC);
        }
    
        public override int GetHashCode()
        {
            int hash = 19;
            hash = hash + (columnA ?? "").GetHashCode();
            hash = hash + (columnB ?? "").GetHashCode();
            hash = hash + columnC.GetHashCode();
            return hash;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All the solutions I have found so far for changing the color of the
I have been searching for a solution to this and so far found nothing
Most of the ASP.NET MVC paging solutions I have found by googling look like
Hi i have been looking for some solutions but i have found nothing... Is
Although I have found some solutions to this problem, none of them refer to
I have found similar problems on the web, but none of the posted solutions
I've searched the Internet and have found some good solutions for teeing STDOUT to
I have question regarding disabling browser caching. I have already found few solutions, and
I have tried a variety of different solutions found on stack and other places
I have Googled a lot and found a lot of solutions, but none of

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.