Here's my LINQ query:
var settingViewModels = from l in settingsByEnvironment["Localhost"]
join d in settingsByEnvironment["Dev"] on l.Key equals d.Key
join p in settingsByEnvironment["Prod"] on d.Key equals p.Key
select new MyKeyValue
{
Key = p.Key,
LocalhostValue = l.Value,
DevValue = d.Value,
ProdValue = p.Value
};
As you see, I've hard coded the three environment Localhost, Dev, and Prod in two parts of my code.
What if tomorrow, I have a new environment? My code is not dynamic.
I tried to use ExpandoObject, but still I cannot have a full dynamic query. Here's the equivalent of my previous LINQ code using ExpandoObject;
// listSettingsEnvLocalhost is of type Tuple<string (environmentName), List<SettingViewModels>>
public void GetSettingsValueForEachEnvironment()
{
var foo = from p in listSettingsEnvLocalhost.Item2
join a in listSettingsEnvDev.Item2 on p.Key equals a.Key
let environmentLocalhost = listSettingsEnvLocalhost.Item1
let environmentDev = listSettingsEnvDev.Item1
select ToExpando(p, a, environmentLocalhost, environmentDev);
}
private dynamic ToExpando(SettingViewModel first, SettingViewModel second, string environmentLocalhost, string environmentDev)
{
dynamic o = new ExpandoObject();
((IDictionary<string, object>)o).Add("Key", first.Key);
((IDictionary<string, object>)o).Add(environmentLocalhost, first.Value);
((IDictionary<string, object>)o).Add(environmentDev, second.Value);
return o;
}
Is an expression tree a solution?