下面的代码将根据重要性顺序将3个单独的表组合成一个表.我希望改进这一点 - 也许通过使用查询语法来避免中间阶段.是否有不同的(更好的?)方法来实现相同的结果?
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")
};
// Order of importance: Upsert > Fields > 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();
Run Code Online (Sandbox Code Playgroud)
LINQPad输出:
Key | Value
------------
f1 | f1-upsert …Run Code Online (Sandbox Code Playgroud)