Felix
Felix

Reputation: 10078

joining two collections in C# or JavaScript

I have two objects of "pseudotype" Hash(int key, list values) that I need to combine into one based on the key. For example,

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

and

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

The result that I need is:

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

(JSON-like format is for readability)

I can do it on the server (C#) or on the client (Javascript/Angular). Is there an aggregate type in C# that has a method that would do that? Or maybe some masterful LINQ expression that does the same?

Or the best way is to have them as Hashtable<int, List<object>>, and join them "manually"?

UPDATE: Based on the answer below, this is a better way to ask the question:

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

Is there some Collection type that would allow me to join two objects instead of going through the loop?

Upvotes: 3

Views: 102

Answers (1)

IndieTech Solutions
IndieTech Solutions

Reputation: 2541

There are couple of ways on how you can achieve that, i am guessing you want to use a json file as a source file, so why not convert everything to objects and deal with them that way, which will provide more flexibility to manipulate them and perform more complication processing if needed.

Here's my draft version:

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

    }

output: 1 a b c 2 apple pear tomato 3 blue red pink 4 x y z

Check this link out

Upvotes: 1

Related Questions