I have two list of the following structure in c#. Each name in <> is a list. I want to join those two lists into one List<Server>. The following pseudo code displays two such lists and how the result should look like:
<Servers> <Servers>
+... +...
+-- Server A,1 +-- Server A,1
| +... | +...
| +--<Maps> | +--<Maps>
| +--Map x | +--Map x
| +... | +...
| +--<Times> | +--<Times>
| +--Time i | +--Time l
| +--Time j | +--<Jumps>
| +--<Jumps> | +--Jump t
| +--Jump s | +--Jump u
| +--Map y | +--Map z
| +... | +...
| +--<Times> | +--<Jumps>
| +-- Time k | +--Jump v
+-- Server B,1 +-- Server B,2
Result should be:
<Servers>
+...
+-- Server A,1
| +...
| +--<Maps>
| +-- Map x
| +...
| +--<Times>
| +--Time i
| +--Time j
| +--Time l
| +--<Jumps>
| +--Jump s
| +--Jump t
| +--Jump u
| +-- Map y
| +...
| +--<Times>
| +--Time k
| +-- Map z
| +...
| +--<Jumps>
| +--Jump v
+-- Server B,1
+-- Server B,2
I tried using full outer join with linq but the result was also not what I wanted, for reasons I didn't understand servers that had the same keys weren't matched and so I always had duplicates of the same server with different data. That's when I stopped trying to do it with linq and just used loops to merge the lists by hand.
The code below gives me the desired result list. For now I will use it until I find a better way to do it. Is there a simple/shorter way doing that with linq? Using lambda expressions?
foreach (Server importServer in oImportList)
{
if (!CoreList.Contains(importServer))
{
CoreList.Add(importServer);
continue;
}
Server coreServer = CoreList.FirstOrDefault(o => o.Equals(importServer));
coreServer.Name = importServer.Name;
coreServer.KZTimerVersion = importServer.KZTimerVersion;
foreach(Map importMap in importServer.Maps)
{
if (!coreServer.Maps.Contains(importMap))
{
coreServer.Maps.Add(importMap);
continue;
}
Map coreMap = coreServer.Maps.FirstOrDefault(o => o.Equals(importMap));
coreMap.Jumps.Concat(importMap.Jumps);
coreMap.Times.Concat(importMap.Times);
}
}