Joining two lists into one

beantownace

Active member
Joined
Feb 15, 2019
Messages
26
Programming Experience
5-10
Hello all,

I have two lists where I want everything in the left list then adding one property value from the right into that left list joined on a value. Here is what I have.

BsonDocument is a collection of name/value pairs so I can get the value such as invoices["CustomerId"]
Both these lists have the CustomerId and what I am trying to get is a List<BsonDocument> everything in the invoices list with one key value of CustomerName joined on the CustomerId from customers.

List<BsonDocument> invoices;
List<BsonDocument> customers

What is the fastest way to try to do this? I can add a key value pair such as this:
invoices.Add("CustomerName", customerName) but I need this for every BsonDocument in invoices.

Thanks All
 
Welcome to trying to make a document based database act like a relational database.

The best bet is to let the database do the work for you on the database side instead of trying to do it in memory on the client site. If possible use DBRef. Otherwise use the manual references.


But if you really must do all the work client side, look at using the Join() and Select() LINQ extension methods to perform the join and then create a new object that has all the combined information that you need.
 
Welcome to trying to make a document based database act like a relational database.

The best bet is to let the database do the work for you on the database side instead of trying to do it in memory on the client site. If possible use DBRef. Otherwise use the manual references.


But if you really must do all the work client side, look at using the Join() and Select() LINQ extension methods to perform the join and then create a new object that has all the combined information that you need.
I know right it totally sucks to work with. Essentially I have repositories from a legacy app where I added the new repository I needed to a MongoDb collection that has the customer name so now just wanting everything from the invoices Left side and adding the one customer name to the right joined.
 
What is the outout supposed to be? A single new BsonDocument? Or a tuple of BsonDocuments associated under a common key?
 
I got the impression that he wants the invoices Mongo collection to eventually be updated with the customer names, but I have been reading questions wrong the past two weeks.
 
Hello all,

I have two lists where I want everything in the left list then adding one property value from the right into that left list joined on a value. Here is what I have.

BsonDocument is a collection of name/value pairs so I can get the value such as invoices["CustomerId"]
Both these lists have the CustomerId and what I am trying to get is a List<BsonDocument> everything in the invoices list with one key value of CustomerName joined on the CustomerId from customers.

List<BsonDocument> invoices;
List<BsonDocument> customers

What is the fastest way to try to do this? I can add a key value pair such as this:
invoices.Add("CustomerName", customerName) but I need this for every BsonDocument in invoices.

Thanks All

To join the two lists based on the "CustomerId" field and add the "CustomerName" property from the "customers" list to the "invoices" list, you can use LINQ to create a new list of BsonDocuments that includes all the properties from the "invoices" list and the "CustomerName" property from the "customers" list.
 
Last edited by a moderator:
Back
Top Bottom