0
{
 "ProductDMList":
   [
    {
     "ProductID" : 1,
     "CabinetList":
        [
         {
          "Min" : 1,
          "Max" : 12
         }
        ]
    },
     {
     "ProductID" : 1,
     "CabinetList":
        [
         {
          "Min" : 16,
          "Max" : 100
         }
        ]
    }
   ]
}

I am using the below code to generate the above list.

List<ProductDM> productDMList = _orderRepo.GetSuggestedList(23, 12);
 for (int i=0;i<productDMList.Count;i++)
  {
   productDMList[i].CabinetList.Add(new InventoryDM {
   Min = productDMList[i].Min,
   Max = productDMList[i].Max
  });
 }

public class ProductDM
  {
    public List<InventoryDM> CabinetList { get; set; }
    public int ProductID { get; set; }
    public double Min { get; set; }
    public double Max { get; set; }
  }
public class InventoryDM
 {
    public Double Min { get; set; }
    public Double Max { get; set; }
 }

How can I join the above 2 lists using ProductID.

Ex : If the ProductID is same I want to create one list and bind all cabiletLists inside it.

Expected Output

{
 "ProductDMList":
   [
    {
     "ProductID" : 1,
     "CabinetList":
        [
         {
          "Min" : 1,
          "Max" : 12
         },
         {
          "Min" : 16,
          "Max" : 100
         }
        ]
     }
   ]
}

I tried AddRange() and Concat() methods. But I was unable to get the above expected result.

Harsha W
  • 3,162
  • 5
  • 43
  • 77

3 Answers3

1

I would propose to use a Dictionary to access already seen Products by their ID and then add the InventoryDM instances as you loop over the un-merged List:

    static void Main(string[] args)
    {
        List<ProductDM> productDMList = new List<ProductDM>()
        {
            new ProductDM()
            {
                ProductID = 1,
                CabinetList = new List<InventoryDM>()
                {
                    new InventoryDM()
                    {
                        Min = 1,
                        Max = 12
                    }
                }
            },
            new ProductDM()
            {
                ProductID = 1,
                CabinetList = new List<InventoryDM>()
                {
                    new InventoryDM()
                    {
                        Min = 16,
                        Max = 100
                    }
                }
            },
        };

        Dictionary<int, ProductDM> dict = new Dictionary<int, ProductDM>();

        foreach(ProductDM product in productDMList)
        {
            if(!dict.ContainsKey(product.ProductID))
            {
                dict.Add(product.ProductID, product);
            }
            else
            {
                dict[product.ProductID].CabinetList.AddRange(product.CabinetList.ToArray());
            }
        }

        Console.ReadKey(true);

    }

dict.Values is then your merged List

Robin B
  • 1,066
  • 1
  • 14
  • 32
1

Maybe this? If i understand what you are asking

var list = new List<ProductDM>();

var result = list.GroupBy(x => x.ProductID)
                 .Select(x => new ProductDM
                     {
                        ProductID = x.Key,
                        Min = x.Min(y => y.Min),
                        Max = x.Max(y => y.Max),
                        CabinetList = x.SelectMany(y => y.CabinetList).ToList()
                     }).ToList();

Enumerable.GroupBy Method (IEnumerable, Func)

Groups the elements of a sequence according to a specified key selector function.

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • What function should I use if I want to bind a string value to the above list. Ex ; ProductID = x.Key, Min = x.Min(y => y.Min), Max = x.Max(y => y.Max), Name= x.Max(y => y.Name), If I want to bind the name as well to the cabinetList what function should I use. I used Min or Max. But it returns nothing to the name – Harsha W May 30 '18 at 08:30
  • How do you mean,bind a string value – TheGeneral May 30 '18 at 08:31
  • Min = x.Min(y => y.Min), Max = x.Max(y => y.Max), Name= x.Max(y => y.Name) If I want to bind the name as well inside the cabinet list, how I can I bind it. MIN() or MAX() functions returns `null`for the name – Harsha W May 30 '18 at 08:31
  • use the First() instead – TheGeneral May 30 '18 at 08:37
  • @HarshaW Name = x.First(); – TheGeneral May 30 '18 at 08:37
  • @HarshaW Name = x.First(y => y.Name); – TheGeneral May 30 '18 at 08:40
0

Javascript solution so easy :)

var tempaa = {
"ProductDMList":
    [
        {
            "ProductID": 1,
            "CabinetList":
                [
                    {
                        "Min": 1,
                        "Max": 12
                    }
                ]
        },
        {
            "ProductID": 1,
            "CabinetList":
                [
                    {
                        "Min": 16,
                        "Max": 100
                    }
                ]
        }
    ]
};

var tempbb = [];

function isExistsInBB(productId, currentProductDM, sourceCabinetList) {

  if (tempbb.length == 0) {
      tempbb.push(currentProductDM);
      return;
  }

  for (var i = 0; i < tempbb.length; i++) {
    var innerItem = tempbb[i];
    if (productId == innerItem.ProductID) {
        innerItem.CabinetList.push(sourceCabinetList);
    } else {
        tempbb.push(currentProductDM);
    }
  }
}

function eachTempaa() {
  for (var i = 0; i < tempaa.ProductDMList.length; i++) {
    var innerItem = tempaa.ProductDMList[i];
    isExistsInBB(innerItem.ProductID, innerItem, innerItem.CabinetList[0]);
  }
  console.log(tempbb);
}

eachTempaa();
MerlinMa
  • 1
  • 2