在 C# 或 JavaScript 中联接两个集合

本文关键字:两个 集合 JavaScript | 更新日期: 2023-09-27 17:56:39

我有两个"伪类型"哈希(int键,列表值)的对象,我需要根据键将它们组合成一个。例如

[{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);
            }
        }

是否有某种集合类型允许我联接两个对象而不是遍历循环?

在 C# 或 JavaScript 中联接两个集合

几种方法可以实现这一点,我猜你想使用 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

查看此链接