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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:42:10+00:00 2026-06-10T16:42:10+00:00

I have already thought about how I’m going to solve this by rolling my

  • 0

I have already thought about how I’m going to solve this by rolling my own solution, but I wondered if .NET already has the functionality for what I’m trying to acheive – if so, I’d rather use something built-in.

Suppose I have two instances of a Widget object, let’s call them PartA and PartB. The information from each has been garnered from two different web services, but both have matching IDs.

PartA
{
    ID: 19,
    name: "Percy",
    taste: "",
    colour: "Blue",
    shape: "",
    same_same: "but different"
}

PartB
{
    ID: 19,
    name: "",
    taste: "Sweet",
    colour: "",
    shape: "Hexagon",
    same_same: "but not the same"
}

I want to merge these to create the following:

Result
{
    ID: 19,
    name: "Percy",
    taste: "Sweet",
    colour: "Blue",
    shape: "Hexagon",
    same_same: "but different"
}

Notice how the value for same_same differs between each, but we consider PartA the master, so the result retains the value but different.

Now to complicate matters:

Suppose we have two lists:

List<Widget> PartA = getPartA();
List<Widget> PartB = getPartB();

Now here’s some pseudocode describing what I want to do:

List<Widget> Result = PartA.MergeWith(PartB).MergeObjectsOn(Widget.ID).toList();
  • 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-10T16:42:12+00:00Added an answer on June 10, 2026 at 4:42 pm

    You could write your own extension method(s), something like this:

    static class Extensions
    {
        public static IEnumerable<T> MergeWith<T>(this IEnumerable<T> source, IEnumerable<T> other) where T : ICanMerge
        {
            var otherItems = other.ToDictionary(x => x.Key);
            foreach (var item in source)
            {
                yield return (T)item.MergeWith(otherItems[item.Key]);
            }
        }
        public static string AsNullIfEmpty(this string s)
        {
            if (string.IsNullOrEmpty(s))
                return null;
            else
                return s;
        }
    }
    

    Where ICanMerge is like:

    public interface ICanMerge
    {
        object Key { get; }
        ICanMerge MergeWith(ICanMerge other);
    }
    

    Implemented e.g. like:

    public class Widget : ICanMerge
    {
        object ICanMerge.Key { get { return this.ID; } }
        int ID {get;set;}
        string taste {get;set;}
        public ICanMerge MergeWith(ICanMerge other)
        {
            var merged = new Widget();
            var otherWidget = (Widget)other;
            merged.taste = this.taste.AsNullIfEmpty() ?? otherWidget.taste;
            //...
            return merged;
        }
    }
    

    Then it’s as simple as PartA.MergeWith(PartB).ToList().

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

Sidebar

Related Questions

I thought this question would have already existed on SO, but then I couldn't
I have tried looking though several of the already asked question about this topic
Yes, I have already asked this question, but the problem is much more specific.
I have already download and install this package to support HTML5 input type http://support.microsoft.com/kb/2468871
I have already googled and searched stackoverflow but I can find nothing related to
I have already created my application on web socket with ASP.net 4.0. I just
I have already read some posts, but no one helped me with my problem.
I'm searching for a high accessing logging solution. My table has this structure: |
I have this example here to illustrate the situation: http://jsfiddle.net/nubrF/40/ If you hold your
I posted this question at ServerFault without responses so far, so I thought about

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.