CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
// No need for Queryables. They're already dictionaries
var englishResources = GetAllResourceValues(1);
var arabicResources = GetAllResourceValues(2);
// Start with all the english words.
// Use dictionary because it has efficient key matching.
var merged = englishResources.ToDictionary(
i => i.Key,
i => new LanguageResourceModel {
Name = i.Key,
EnglishValue = i.Value.Value
});
// Now merge the arabic ones.
// You could LINQ-ify the whole thing, but it's not gonna make it more efficient.
LanguageResourceModel found;
foreach (var item in arabicResources)
{
// If value already exists, update it
if (merged.TryGetValue(item.Key, out found))
found.ArabicValue = item.Value.Value;
else // Otherwise, add a new one
merged[item.Key] = new LanguageResourceModel {
Name = item.Key,
ArabicValue = item.Value.Value
};
}
//debug print
merged.Values.ToList().ForEach(i => Console.WriteLine($"{i.Name}, English={i.EnglishValue}, Arabic={i.ArabicValue}"));
}
// Produce some values for testing
public static Dictionary<string, KeyValuePair<int,string>> GetAllResourceValues(int languageId){
if (languageId == 1) //english
return new Dictionary<string, KeyValuePair<int, string>> {
{"one", new KeyValuePair<int,string>(1, "one") },
{"two", new KeyValuePair<int,string>(1, "two") },
{"three", new KeyValuePair<int,string>(1, "three") },
};
else //spanish, cause I don't know arabic
return new Dictionary<string, KeyValuePair<int, string>> {
{"one", new KeyValuePair<int,string>(1, "uno") },
{"four", new KeyValuePair<int,string>(1, "quatro") },
};
}
// and simple class because I'm lazy
public class LanguageResourceModel{ public string Name, EnglishValue, ArabicValue; }
}