Combining two collections in C # or JavaScript
I have two objects "pseudo" 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 I want is:
[{1, {a, b, c}},
{2, {apple, pear, tomato}},
{3, {blue, red, pink, red}},
{4, {x, y, z}}]
(JSON-like format is readable)
I can do this on the server (C #) or client (Javascript / Angular). Is there an aggregate type in C # that has a method that would do this? Or maybe some masterful LINQ expression that does the same thing?
Or is the best way to use them like Hashtable<int, List<object>>
and join them "manually"?
UPDATE: Based on the answer below, this is the best 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 type of collection that would allow me to join two objects instead of going through a loop?
source to share
There are several ways how you can achieve this, I am assuming you want to use a json file as your source file, so why not convert everything to objects and deal with them in a way that provides more flexibility to manipulate them and do additional handling complications, if necessary.
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 abc 2 apple pear tomato 3 blue red pink 4 xyz
Check the link outside
source to share