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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:02:54+00:00 2026-06-04T13:02:54+00:00

This seems fairly easy but I can’t focus. Let’s say I have got 2

  • 0

This seems fairly easy but I can’t focus.
Let’s say I have got 2 classes defined like this:

class Class1
{
    public int Document;
    public decimal Amount;
}

class Class2
{
    public string Document;
    public decimal Amount;
}

There are IEnumerable<Class1> result1 and IEnumerable<Class2> result2 defined and filled with data.

I have a third class defined like this:

class ResultClass
{
    public string Document;
    public decimal? AmountFromClass1;
    public decimal? AmountFromClass2;
}

I want a LINQ query which selects some fields from the first result and some fields from the second result and puts them in a third result defined as List<ResultClass> result.
For example, if I have the first result (result1) populated like this:

  Document    Amount
     1         1.55
     2         2.55

and the second result (result2) populated like this:

  Document    Amount
   "d-1"        1.22
   "d-2"        2.22

I want to have the final result populated something like this (order doesn’t matter):

  Document    AmountFromClass1    AmountFromClass2
    "1"             1.55                null
   "d-1"            null                1.22
    "2"             2.55                null
   "d-2"            null                2.22

Currently I do this using two foreach statements, like this:

List<ResultClass> result = new List<ResultClass>();

foreach (var o in result1)
    result.Add(new ResultClass 
    { 
        Document = o.Document.ToString(), 
        AmountFromClass1 = o.Amount, 
        AmountFromClass2 = null 
    });

foreach (var o in result2)
    result.Add(new ResultClass 
    { 
        Document = o.Document, 
        AmountFromClass1 = null, 
        AmountFromClass2 = o.Amount 
    });

But I would like to do this using LINQ.
Is it possible?

EDIT: TO REFINE THE QUESTION (also added the entity-framework tag)
As this is just a code example, I am aware that there might be no real benefits of doing this in a single LINQ statement as it would do the same.
However, I am looking for a performance improvement where result1 and result2 are actually IQueryables from LINQ-to-Entities, in a way so I could do something like this:

List<ResultClass> MyFunc()
{
    Database DB = new Database(); // object context
    var result1 = DB.Class1s.Where(...);
    var result2 = DB.Class2s.Where(...);
    return SomeMagic()
        .Select(x => new ResultFunc { ... })
        .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-04T13:02:56+00:00Added an answer on June 4, 2026 at 1:02 pm

    Project each item in both collections to a ResultClass object and concatenate the two resulting enumerables:

    var query1 = from o in result1
                 select new ResultClass
                 {
                     Document = o.Document.ToString(),
                     AmountFromClass1 = o.Amount
                 };
    
    var query2 = from o in result2
                 select new ResultClass
                 {
                     Document = o.Document,
                     AmountFromClass2 = o.Amount
                 };
    
    var result = query1.Concat(query2).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it should be fairly simple, but for some reason I can't
This seems like a fairly obvious question, but I haven't been able to think
This seems like it would be fairly simple, but I've been unable to find
This seems like it should be obvious but I can't figure it out. Suppose
I'd like to count and group rows by specific values. This seems fairly simple,
This seems to be a trivial question but I got hung on it for
This seems like a basic question, but I haven't found any clear answers. Essentially,
This seems like a basic question, but it's still bugging me. Why doesn't MyObject
This is a fairly easy one, but I cant seem to figure it out.
So im sure this is probably a fairly easy question but I am stumped

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.