-1

I have two different object models(SellerOrder and BuyerOrders) with different properties except one(OrderId). I am getting list of data of both models from different files and I want to get both list to be merged in another object Model(OrderDetails) list. Here are the details.

I have models as below:

Public class SellerOrder
    {
        public string OrderId { get; set; }
        public string MerchantOrderId { get; set; }
        public DateTime PurchaseDate { get; set; }
        public DateTime Amount{ get; set; }
    }

Public class UserOrder
    {
        public string OrderId { get; set; }
        public string BuyerId { get; set; }
        public string BuyerName { get; set; }
        public string BuyerAddress{ get; set; }
    }

Public class OrderDetails
    {
        public string OrderId { get; set; }
        public string MerchantOrderId { get; set; }
        public DateTime PurchaseDate { get; set; }
        public DateTime Amount{ get; set; }
        public string BuyerId { get; set; }
        public string BuyerName { get; set; }
        public string BuyerAddress{ get; set; }
    }

My data is coming like below:

 //getting data from two different places
 List<SellerOrder> sellerOrderList = place1.GetSellerData(); 
 List<BuyerOrder>  buyerOrderList = place2.GetBuyerData();

I want to get the result like below:

 SellerOderList:
{ OrderId = "1", MerchantOrderId = "1s1", PurchaseDate = "2020/12/04", Amount = "10.00" }
{ OrderId = "2", MerchantOrderId = "1s2", PurchaseDate = "2020/12/04", Amount = "10.00" }
{ OrderId = "3", MerchantOrderId = "1s3", PurchaseDate = "2020/12/04", Amount = "10.00" }
{ OrderId = "4", MerchantOrderId = "1s4", PurchaseDate = "2020/12/04", Amount = "10.00" }
{ OrderId = "5", MerchantOrderId = "1s5", PurchaseDate = "2020/12/04", Amount = "10.00" }

 BuyerOderList:
{ OrderId = "1", BuyerId = "1b1", BuyerName = "Abc", BuyerAddress = "Toronto" }
{ OrderId = "2", BuyerId = "1b2", BuyerName = "Efg", BuyerAddress = "Toronto" }
{ OrderId = "3", BuyerId = "1b3", BuyerName = "Hij", BuyerAddress = "Toronto" }
{ OrderId = "4", BuyerId = "1b4", BuyerName = "Lmn", BuyerAddress = "Toronto" }
{ OrderId = "5", BuyerId = "1b5", BuyerName = "Opq", BuyerAddress = "Toronto" }


OrderDetailsList:

{ OrderId = "1", MerchantOrderId = "1s1", PurchaseDate = "2020/12/04", Amount = "10.00", BuyerId = "1b1", BuyerName = "Abc", BuyerAddress = "Toronto" }
{ OrderId = "2", MerchantOrderId = "1s2", PurchaseDate = "2020/12/04", Amount = "10.00", BuyerId = "1b2", BuyerName = "Efg", BuyerAddress = "Toronto" }
{ OrderId = "3", MerchantOrderId = "1s3", PurchaseDate = "2020/12/04", Amount = "10.00" , BuyerId = "1b3", BuyerName = "Hij", BuyerAddress = "Toronto" }
{ OrderId = "4", MerchantOrderId = "1s4", PurchaseDate = "2020/12/04", Amount = "10.00" , BuyerId = "1b4", BuyerName = "Lmn", BuyerAddress = "Toronto" }
{ OrderId = "5", MerchantOrderId = "1s5", PurchaseDate = "2020/12/04", Amount = "10.00", BuyerId = "1b5", BuyerName = "Opq", BuyerAddress = "Toronto" }

I want to get Seller Order details and Buy Order details to be merged in Order Details List.

4
  • Does this answer your question? Merge contents of multiple lists of custom objects - C# Commented Dec 4, 2020 at 21:36
  • No. Actually, the link you sent is having multiple lists with same model and in my situation it has 3 different models(with one unique property in all) to work with. Commented Dec 4, 2020 at 23:12
  • And yet, this answer uses Join in much the same way as the accepted answer here. Maybe the key is to not get caught up on particulars... Commented Dec 4, 2020 at 23:15
  • Accepted answer in your suggested link is very complex and little different. It is merging first and then concatenate which I think is not efficient as I can achieve it in one shot by below answer according to my problem. Commented Dec 7, 2020 at 18:13

1 Answer 1

2

Try Enumerable.Join:

var orderDetailsList: = sellerOrderList.Join(buyerOrderList, arg => arg.OrderId, arg => arg.OrderId,
    (first, second) => new OrderDetails {
        OrderId = first.OrderId, 
        MerchantOrderId = first.MerchantOrderId,
        PurchaseDate = first.PurchaseDate,
        Amount = first.Amount,
        BuyerId = second.BuyerId,
        BuyerName = second.BuyerName,
        BuyerAddress = second.BuyerAddress
        });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.