【问题标题】:Merge 2 or more inner lists合并 2 个或更多内部列表
【发布时间】:2018-05-30 07:50:12
【问题描述】:
{
 "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 加入上述两个列表。

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

预期输出

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

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

【问题讨论】:

标签: c# list merge


【解决方案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)

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

【讨论】:

  • 如果我想将一个字符串值绑定到上面的列表,我应该使用什么函数。前任 ; ProductID = x.Key, Min = x.Min(y => y.Min), Max = x.Max(y => y.Max), Name= x.Max(y => y.Name), 如果我还想将名称绑定到 cabinetList 我应该使用什么功能。我使用了最小值或最大值。但它没有返回任何名称
  • 什么意思,绑定一个字符串值
  • Min = x.Min(y => y.Min), Max = x.Max(y => y.Max), Name= x.Max(y => y.Name) 如果我想在内阁列表中绑定名称,我该如何绑定它。 MIN() 或 MAX() 函数为名称返回 null
  • 使用 First() 代替
  • @HarshaW Name = x.First();
【解决方案2】:

我建议使用字典通过它们的 ID 访问已经看到的产品,然后在循环未合并的列表时添加 InventoryDM 实例:

    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 就是你的合并列表

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      相关资源
      最近更新 更多