-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.

Vatsal Patel
  • 571
  • 5
  • 11
  • Does this answer your question? [Merge contents of multiple lists of custom objects - C#](https://stackoverflow.com/questions/10798825/merge-contents-of-multiple-lists-of-custom-objects-c-sharp) – Heretic Monkey Dec 04 '20 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. – Vatsal Patel Dec 04 '20 at 23:12
  • And yet, [this answer uses Join in much the same way as the accepted answer here](https://stackoverflow.com/a/10799080/215552). Maybe the key is to not get caught up on particulars... – Heretic Monkey Dec 04 '20 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. – Vatsal Patel Dec 07 '20 at 18:13

1 Answers1

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
        });
SzybkiDanny
  • 204
  • 2
  • 11