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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:17:53+00:00 2026-06-09T22:17:53+00:00

I have two lists (ObservableCollections) of following class: public class Data { public string

  • 0

I have two lists (ObservableCollections) of following class:

public class Data
{
    public string Key { get; set; }
    public string Value { get; set; }
}

One representing old objects (listA) and second representing updated ones (listB). I want to merge new data from listB to listA without breaking any references. More precisely I want to do the following:

  • Remove from listA all objects that do not exist in listB (objects are compared by Key property)
  • Add to listA all objects from listB that do not exist in listA
  • Update Value property for all objects in listA that are present in both lists

Can you propose some efficient way to do that? My solution is big and seems very ineffective.

Update:
Current solution is:

public void MergeInstanceList(List<Instance> instances)
{
    var dicOld = Instances.ToDictionary(a => a.Ip);
    var dicNew = instances.ToDictionary(a => a.Ip);
    var forUpdate = instances.Intersect(Instances, new Comparer()).ToList();
    Instances.Where(a => !dicNew.Keys.Contains(a.Ip)).ToList().ForEach(a => Instances.Remove(a));
    instances.Where(a => !dicOld.Keys.Contains(a.Ip)).ToList().ForEach(a => Instances.Add(a));
    forUpdate.ForEach(a => dicOld[a.Ip].Name = a.Name);
}
public class Comparer : IEqualityComparer<Instance>
{

    public bool Equals(Instance x, Instance y)
    {
        return x.Ip == y.Ip;
    }

    public int GetHashCode(Instance obj)
    {
        return obj.Ip.GetHashCode();
    }
}
  • 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-09T22:17:55+00:00Added an answer on June 9, 2026 at 10:17 pm
    var listA = ...;
    var listB = ...;
    
    var itemsToRemove = new HashSet<Data>(listA.Except(listB));
    var itemsToAdd = listB.Except(listA);
    var itemsToUpdate = listA.Join(listB, a => listA.Key, b => listB.Key, 
                (a, b) => new
                {
                    First = a,
                    Second = b
                });
    
    listA.AddRange(itemsToAdd);
    listA.RemoveAll(item => itemsToRemove.Contains);
    foreach(var pair in itemsToUpdate)
    {
      //set properties in First to be that of second
    }
    

    As is mentioned in another answer, you will need to create a custom comparer and pass it into the two Except methods for them to work properly, or you will need to override the Equals and GetHashCode methods to be based off of just Key.

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

Sidebar

Related Questions

I have two classes: public class Person { public string FirstName { get {
I have a class called Book; class Book { public string Name { get;
I have two lists as List <BillDetails>= new List<BillDetails>(); List<Header>=new List<Header>(); Public class Billdetails
I have two lists in my class and i want to expose one more
I have two lists. Ex: List<string> expected = new List<string>(); expected.Add( a ); expected.Add(
I have two lists. They are sortable but not connected (elements of list one
We have two lists: a=['1','2','3','4'] b=['2','3','4','5'] How to get a list with elements that
I have two lists from which I have to get two values(MyCaption and MyValue).
Suppose I have two comboboxes. One for the lower-value and one for the upper
I have two lists one displays cars company and the second one displays all

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.