簡體   English   中英

用C#或JavaScript加入兩個集合

[英]joining two collections in C# or JavaScript

我有兩個“假型”哈希(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);
            }
        }

是否有一些Collection類型允許我加入兩個對象而不是通過循環?

有幾種方法可以實現這一點,我猜你想使用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 abc 2蘋果梨番茄3藍紅色粉紅色4 xyz

檢查此鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM