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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:51:58+00:00 2026-05-31T17:51:58+00:00

I got two collections of objects. For example: List<Foo> firstFoos = new List<Foo>(); List<Foo>

  • 0

I got two collections of objects.

For example:

List<Foo> firstFoos = new List<Foo>();
List<Foo> secondFoos = new List<Foo>();

firstFoos.Add(new Foo() { Id = 1, ValueA = 10, ValueB = 15 });
firstFoos.Add(new Foo() { Id = 2, ValueA = 20, ValueB = 25 });
firstFoos.Add(new Foo() { Id = 3, ValueA = 30, ValueB = 35 });
firstFoos.Add(new Foo() { Id = 4, ValueA = 40, ValueB = 45 });

secondFoos.Add(new Foo() { Id = 1, ValueA = 100, ValueB = 150 });
secondFoos.Add(new Foo() { Id = 2, ValueA = 200, ValueB = 250 });

Using LINQ, how can I merge the two collection overriding firstFoos by secondFoos which have the same ID?

Expected result is:

|   Id    | ValueA | ValueB |
|---------|--------|--------|
|    1    |   100  |   150  |
|    2    |   200  |   250  |
|    3    |   30   |   35   |
|    4    |   40   |   45   |

Please note that this example case has only two value columns (ValueA and ValueB), but an actual case could have many more.

  • 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-31T17:51:59+00:00Added an answer on May 31, 2026 at 5:51 pm

    I’d convert it to an Id -> Foo dictionary, and then just update with a regular foreach:

    var fooDict = firstFoos.ToDictionary(foo => foo.Id, foo => foo);
    foreach (var foo in secondFoos) 
        fooDict[foo.Id] = foo;
    var newFoos = fooDict.Values.OrderBy(foo => foo.Id).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got two List objects and I want to pair them up, just like
I've got two objects: User and Permission, where User has a Collection. In my
I've got a settings object for my app that has two collections in it.
I've got two collections (generic Lists), let's call them ListA and ListB. In ListA
I've got 2 entities in JPA: Entry and Comment. Entry contains two collections of
I've got two ListBox's with objects as an ItemsSource populating them. Right now, I'm
I've got two collections, ObservableCollection<Lap> and a ObservableCollection<Racer> where Lap holds lap data of
I've got two objects (@stacks and @stories) with has_many relationships (through @anthologies). Stories can
I got two tables Product(Productname VARCHAR(10), Rating REAL) and Review(RatingID INT, Productname VARCHAR(10), Rating
I got two projects running, both written in PHP. Now I want to merge

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.