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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:16:56+00:00 2026-06-04T17:16:56+00:00

I have a class Project as public class Project { public int ProjectId {

  • 0

I have a class Project as

public class Project 
{   public int ProjectId { get; set; }
    public string ProjectName { get; set; }
    public string Customer { get; set; }
    public string Address{ get; set; }
}

and I have 3 lists

List<Project> lst1; List<Project> lst2; List<Project> lst3;

lst1 contains Person objects with ProjectId and ProjectName.

ProjectId =1, ProjectName = "X", Customer = null, Address = null

ProjectId =2, ProjectName = "Y", Customer = null, Address = null

lst2 contains Person objects with ProjectId and Customer

ProjectId =1,ProjectName = null, Customer = "c1", Address = null

ProjectId =2,ProjectName = null, Customer = "c2", Address = null
, and

lst3 contains Person objects with ProjectId and Address

ProjectId = 1, ProjectName = null, Customer =null, Address = "a1"

ProjectId = 2, ProjectName = null, Customer =null, Address = "a2".

Considering there are multiple such records in each list and ProjectId is Uniqe for each project, How can I merge/combine these list to get one list with merged objects

ProjectId=1, ProjectName="X", Customer="c1", address="a1"

ProjectId=2, ProjectName="Y", Customer="c2", address="a2"

I found thse links similar and tried with it but could not meet the results

Create a list from two object lists with linq

How to merge two lists using LINQ?

Thank You.

  • 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-04T17:16:57+00:00Added an answer on June 4, 2026 at 5:16 pm

    This could be done in a multi-step approach pretty simply. First, define a Func<Project, Project, Project> to handle the actual record merging. That is, you are defining a method with a signature equivalent to public Project SomeMethod(Project p1, Project p2). This method implements the merging logic you outlined above. Next, we concatenate the elements of the lists together before grouping them by ProjectId, using our merge delegate as the an aggregate function in the overload of GroupBy which accepts a result selector:

    Func<Project, Project, Project> mergeFunc = (p1,p2) => new Project
        {
            ProjectId = p1.ProjectId,
            ProjectName = p1.ProjectName == null ? p2.ProjectName : p1.ProjectName,
            Customer = p1.Customer == null ? p2.Customer : p1.Customer,
            Address = p1.Address == null ? p2.Address : p1.Address    
        };
    
    var output = lst1.Concat(lst2).Concat(lst3)
                     .GroupBy(x => x.ProjectId, (k, g) => g.Aggregate(mergeFunc)); 
    

    Here’s a quick and dirty test of the above logic along with output:

    List<Project> lst1; List<Project> lst2; List<Project> lst3;
    lst1 = new List<Project> 
        {
            new Project { ProjectId = 1, ProjectName = "P1" },
            new Project { ProjectId = 2, ProjectName = "P2" },
            new Project { ProjectId = 3, ProjectName = "P3" }
        };
    lst2 = new List<Project>
        {
            new Project { ProjectId = 1, Customer = "Cust1"},
            new Project { ProjectId = 2, Customer = "Cust2"},
            new Project { ProjectId = 3, Customer = "Cust3"}
        };
    lst3 = new List<Project>
        {
            new Project { ProjectId = 1, Address = "Add1"},
            new Project { ProjectId = 2, Address = "Add2"},
            new Project { ProjectId = 3, Address = "Add3"}
        };
    
    Func<Project, Project, Project> mergeFunc = (p1,p2) => new Project
        {
            ProjectId = p1.ProjectId,
            ProjectName = p1.ProjectName == null ? p2.ProjectName : p1.ProjectName,
            Customer = p1.Customer == null ? p2.Customer : p1.Customer,
            Address = p1.Address == null ? p2.Address : p1.Address    
        };
    
    var output = lst1
        .Concat(lst2)
        .Concat(lst3)
        .GroupBy(x => x.ProjectId, (k, g) => g.Aggregate(mergeFunc));
    
    IEnumerable<bool> assertedCollection = output.Select((x, i) => 
        x.ProjectId == (i + 1) 
        && x.ProjectName == "P" + (i+1) 
        && x.Customer == "Cust" + (i+1) 
        && x.Address == "Add" + (i+1));
    
    Debug.Assert(output.Count() == 3);  
    Debug.Assert(assertedCollection.All(x => x == true));
    

    — output —

    IEnumerable<Project> (3 items)   
    ProjectId ProjectName Customer Address 
    1 P1 Cust1 Add1 
    2 P2 Cust2 Add2 
    3 P3 Cust3 Add3 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have entity class: public class Project { public virtual int Id { get;
I have the following entity model: public class Project { [Key] public int ProjectID
Given class: class SomeClass { public int intdata {get;set;} public string stringdata {get;set;} public
I have a domain model: public class Project { [Key] public int ProjectID {
I have a library with an Interface. Public Interface Progress { int ProgressValue{get;set;}, string
Given domain model... public class Entity { public int Id { get; set; }
Given the class: public class Item { [Key] public int ID { get; set;
I have the following code in my ASP.NET project public sealed class IoC {
I have in my WPF project file RssInfo.cs in which I have public class
This is a MVC 3 project. Just for testing, I have public class MyRoleProvider

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.