【问题标题】:joining two collections in C# or JavaScript在 C# 或 JavaScript 中加入两个集合
【发布时间】:2015-08-31 16:57:59
【问题描述】:

我有两个“伪类型”Hash(int key, list values) 对象,我需要根据键将它们合并为一个。例如,

[{1, {a, b, c}},
 {2, {apple, pear}},
 {3, {blue, red}}]

[{2, {tomato}},
 {3, {pink, red}},
 {4, {x, y, z}}]

我需要的结果是:

[{1, {a, b, c}},
 {2, {apple, pear, tomato}},
 {3, {blue, red, pink, red}},
 {4, {x, y, z}}]

(类似 JSON 的格式是为了便于阅读)

我可以在服务器 (C#) 或客户端 (Javascript/Angular) 上执行此操作。 C# 中是否有一个聚合类型有一个方法可以做到这一点?或者也许是一些能做同样事情的高超的 LINQ 表达式?

或者最好的方法是让他们成为Hashtable<int, List<object>>,然后“手动”加入他们?

更新:根据下面的答案,这是提出问题的更好方法:

        Dictionary<int, string[]> Dict1 = new Dictionary<int, string[]>();
        Dict1.Add(1, new string[] { "a", "b", "c" });
        Dict1.Add(2, new string[] { "apple", "pear" });
        Dict1.Add(3, new string[] { "blue", "red" });
        Dictionary<int, string[]> Dict2 = new Dictionary<int, string[]>();
        Dict2.Add(2, new string[] { "tomato" });
        Dict2.Add(3, new string[] { "pink", "red" });
        Dict2.Add(4, new string[] { "x", "y", "z" });

        foreach (var item in Dict2) {
            if (Dict1.ContainsKey(item.Key)) {
                Dict1[item.Key] = Dict1[item.Key].Concat(item.Value).ToArray();
            } else {
                Dict1.Add(item.Key, item.Value);
            }
        }

是否有一些 Collection 类型可以让我加入两个对象而不是通过循环?

【问题讨论】:

  • {a, b, c} 是什么意思?它应该是[a,b,c] 的数组吗?或者你的意思是 a 有对象 {a: somevalue, b: somevalue, c: somevalue }
  • 是的,我应该放 [] (我从object[] x = new object[] {a, b, c} 剪切和粘贴。但如果有限制,我可以做任何一种方式......

标签: javascript c# linq collections


【解决方案1】:

有几种方法可以实现这一点,我猜你想使用 json 文件作为源文件,所以为什么不将所有内容都转换为对象并以这种方式处理它们,这将提供更大的灵活性操作它们并在需要时执行更复杂的处理。

这是我的草稿版本:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {

      List<JsonModel> ListJson1 = new List<JsonModel>();
      ListJson1.Add(new JsonModel(1, new List<string>(new string[] {"a", "b", "c"})));
      ListJson1.Add(new JsonModel(2, new List<string>(new string[] {"apple", "pear"})));
      ListJson1.Add(new JsonModel(3, new List<string>(new string[] {"blue", "red"})));

      List<JsonModel> ListJson2 = new List<JsonModel>();
      ListJson2.Add(new JsonModel(2, new List<string>(new string[] {"tomato"})));
      ListJson2.Add(new JsonModel(3, new List<string>(new string[] {"pink", "red"})));
      ListJson2.Add(new JsonModel(4, new List<string>(new string[] {"x", "y", "z"})));

        List<JsonModel> result = ListJson1.Concat(ListJson2)
                                .ToLookup(p => p.Index)
                                .Select(g => g.Aggregate((p1,p2) =>  
                                                         new JsonModel(p1.Index,p1.Names.Union(p2.Names)))).ToList();

        foreach(var item in result)
        {
        Console.WriteLine(item.Index);
            foreach(var Item in item.Names)
                Console.WriteLine(Item);
        }
    }


}


public class JsonModel
    {
      public  int Index;//you can use your private set and public get here
      public  IEnumerable<string> Names;//you can use your private set and public get here

      public JsonModel(int index, IEnumerable<string> names)
      {
        Index = index;
        Names = names;
      }

    }

输出:1 a b c 2 苹果梨番茄 3 蓝色红色粉色 4 x y z

检查这个link out

【讨论】:

  • 我在看你的小提琴 - 似乎第二个字典中的值没有附加......例如key=1时,值是applepear;不是tomato。我错过了什么吗?
  • GroupBy 给出 KeyValuePair>;所以同样的问题只会被推到 GroupBy 结果中
  • 太棒了!鉴于我从未使用过 Lookup、Aggregate 或 Union - 我永远无法自己想出这个 :)
猜你喜欢
  • 2012-09-15
  • 2015-04-09
  • 2020-05-12
  • 2018-06-03
  • 2013-03-11
  • 2017-08-10
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多