合并2个或更多内部列表

时间:2018-05-30 07:50:13

标签: c# list merge

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

我使用以下代码生成上面的列表。

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; }
 }

如何使用ProductID加入上述2个列表。

例如:如果ProductID相同,我想创建一个列表并绑定其中的所有cabiletLists

预期产出

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

我尝试了AddRange()Concat()方法。但我无法得到上述预期结果。

3 个答案:

答案 0 :(得分:1)

我建议使用Dictionary来访问已经看过的产品的ID,然后在循环未合并的List时添加InventoryDM实例:

dict.Values

$arrNotificationMessage = array( 'title'=>'Test', 'text'=>"Testing Notification", 'sound' => "mySound", 'priority'=>"high" ); $extraData = array( 'any_extra_data' =>"any data" ); $deviceToken = "User device FCM token id"; $ch = curl_init("https://fcm.googleapis.com/fcm/send"); $header=array('Content-Type: application/json', "Authorization: key=YOUR_KEY"); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false ); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch, CURLOPT_POSTFIELDS, "{ \"notification\": ".json_encode($notificationMessage).", \"data\":" . json_encode($extraData) . ", \"to\" : ".json_encode($deviceToken)."}"); $result = curl_exec($ch); curl_close($ch); if ($result === FALSE) { //log_message("DEBUG", 'Curl failed: ' . curl_error($ch)); } else{ $result = json_decode($result); if($result->success ===1){ return true; } else{ return false; } } 则是您合并的列表

答案 1 :(得分:1)

也许这个?如果我明白你在问什么

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)

  

根据指定的键对序列的元素进行分组   选择器功能。

答案 2 :(得分:0)

Javascript解决方案非常简单:)

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();