将多个词典合二为一

时间:2018-06-05 11:45:09

标签: c# .net dictionary

假设我有两个变量,它们都是 var json = '[{"Status":"Success","Data":[{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"31","WishListName":"General","WishListItems":[{"WishListItemID":"3","ItemCode":"4414082000005","ItemName":"Notebook 4414-082","Image":"http://aljeel.gct.om/ProductsImages/4414082000005_MT1.jpg","ItemPrice":0.500,"CreatedDate":"25/05/2018"}]},{"UserID":null,"UserName":null,"EmailID":null,"EmailIDExists":false,"Active":null,"Country":null,"WishListID":"24","WishListName":"Default","WishListItems":[]}],"Message":null}]'; var obj = JSON.parse(json)[0].Data; obj.data = obj.WishListItems; delete obj.WishListItems; json = JSON.stringify([obj]); console.log("FINAL JSON " + (json));类型。

Dictionary<int, int>

两个词典都可能包含相同的键。

我希望它们合并为一个Dictionary<int, int> d1 = new Dictionary<int, int> { ... }; Dictionary<int, int> d2 = new Dictionary<int, int> { ... };

这样做的优雅方式是什么?

我尝试过以下方法:

Dictionary<int, int> combinedDictionary

但遗憾的是,在尝试返回此组合变量时,我收到以下错误:

  

无法转换表达式类型&#39; System.Collections.Generic.IEnumerable&gt;&#39;返回类型&#39; System.Collections.Generic.Dictionary&#39;。

是否可以像这样安全地施放它?

var combinedDictionary = d1.Concat(d2.Where(x => !d1.Keys.Contains(x.Key)));

提前感谢!

1 个答案:

答案 0 :(得分:1)

Concat返回context.FindAllAsync<MyEntity>(1, 2, "foo")。您想调用ToDictionary将其转换为字典。

IEnumerable<KeyValuePair<TKey, TValue>>