In the code below, 3 separate tables will be merged into one, based on the order of importance. I want to improve this - perhaps using query syntax to avoid an intermediate stage. Are there different (better?) Ways to achieve the same result?
var upsert = new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("f1","f1-upsert"),
new KeyValuePair<string, string>("f6","f6-upsert")
};
var fields = new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("f3","f3-fields"),
new KeyValuePair<string, string>("f4","f4-fields"),
new KeyValuePair<string, string>("f6","f6-fields")
};
var server = new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("f1","f1-server"),
new KeyValuePair<string, string>("f2","f2-server"),
new KeyValuePair<string, string>("f5","f5-server")
};
var stage = upsert.Concat(fields.Where(f=> !upsert.Any(u=>u.Key==f.Key)));
var final = stage.Concat(server.Where(s=> !stage.Any(j=>j.Key==s.Key))).OrderBy(o=>o.Key);
final.Dump();
LINQPad Output:
Key | Value
------------
f1 | f1-upsert
f2 | f2-server
f3 | f3-fields
f4 | f4-fields
f5 | f5-server
f6 | f6-upsert
source
share