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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:12:13+00:00 2026-05-13T06:12:13+00:00

I have 2 lists and I need to combine the joining values from A

  • 0

I have 2 lists and I need to combine the joining values from A and B, but also include the values from A and B that don’t match the join.

class TypeA
{
    public string Key { get; set; }
    public int ValueA { get; set; }
}

class TypeB
{
    public string Key { get; set; }
    public int ValueB { get; set; }
}

class TypeAB
{
    public string Key { get; set; }
    public int ValueA { get; set; }
    public int ValueB { get; set; }
}

var listA = new List<TypeA>
{
    new TypeA { Key = "one", Value = 1 },
    new TypeA { Key = "two", Value = 2 },
};

var listB = new List<TypeB>
{
    new TypeB { Key = "two", Value = 2 },
    new TypeB { Key = "three", Value = 3 },
};

I want these lists combined to equal this:

var listAB = new List<TypeAB>
{
    new TypeAB { Key = "one", ValueA = 1, ValueB = null },
    new TypeAB { Key = "two", ValueA = 2, ValueB = 2 },
    new TypeAB { Key = "three", ValueA = null, ValueB = 3 },
};

What is a Linq statement that will do this? I’ve been playing around and can’t quite get there. I can get almost there by doing a left outer join on A to B and Union that to a left outer join on B to A, but I get duplicate Intersection values.

Update

Here is what I did based on George’s answer:

var joined =
    ( from a in listA
      join b in listB
        on a.Key equals b.Key
        into listBJoin
      from b in listBJoin.DefaultIfEmpty( new TypeB() )
      select new TypeAB
      {
        Key = a.Key,
        ValueA = a.ValueA,
        ValueB = b.ValueB,
      } ).Union(
        from b in listB
        where !listA.Any( d => d.Key == b.Key )
        select new TypeAB
        {
            Key = b.Key,
            ValueB = b.ValueB,
        }
        ).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-05-13T06:12:14+00:00Added an answer on May 13, 2026 at 6:12 am

    Edit: Fixed up. This is sloppy but it should work.

        listA  //Start with lisA
    .Where(a=>!listB.Any(b=>a.Key == b.Key))  // Remove any that are duplicated in listB
    .Select(a => new TypeAB() { Key=a.Key, ValueA=a.Value})  // Map each A to an AB
    .Union(
      listB.Select(b => {
          var correspondingA = listA.FirstOrDefault(a => a.Key == b.Key);  //Is there an a that corresponds?
        return new TypeAB() { Key=b.Key, 
          ValueB=b.Value,  //Value B is easy
          ValueA= correspondingA!=null ? (int?)correspondingA.Value : null  //If there is an A than map its value
        };
      })
    )
    

    As an aside, if you’re using this as some sort of domain operation, TypeA and TypeB should probably be based on some sort AorBIsAConceptThatHasMeaningInTheDomain base class. That’s just a general rule whenever you find yourself combining lists. If no such concept exists, then you probably don’t need to be combining the lists.

    On the other hand, if you’re doing this as part of a mapping – such as mapping domain objects to UI – you might be able to simplify your code somewhat by using anonymous types instead of a TypeAB class. (Or maybe not, this one is up to personal preference)

    Edit Edit
    Here’s a slightly more intellectually interesting answer using hashes

            var listAB = listA.Cast<object>().Union(listB.Cast<object>()).ToLookup(x => x is TypeA ? (x as TypeA).Key : (x as TypeB).Key)
                    .Select(kv => {
                        var a = kv.FirstOrDefault(x => x is TypeA) as TypeA;
                        var b = kv.FirstOrDefault(x => x is TypeB) as TypeB;
                        return new TypeAB() {
                            Key = kv.Key,
                            ValueA = a != null ? (int?)a.Value : null,
                            ValueB = b != null ? (int?)b.Value : null
                        };
                    }).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two lists that i need to combine where the second list has
I'm completely stumped on this one. I have three different lists that need to
I have a list of values such as 12000,12345,123456 that need to be converted
I have two unsorted lists and I need to produce another list which is
I have a List<InputField> but I need a List<IDataField> Is there a way to
I have a list wrapper that maintains two Tstringlists and a TClassList I need
I have a list/queue of 200 commands that I need to run in a
I have a list of n GUIDs and I need to hash them into
I have a list of error codes I need to reference, kinda like this:
I have a list of objects which need to be output 2 items per

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.