-1

比较两本词典的最佳方法是什么?其中的项目“应该”始终处于相同的顺序。

dictionary<string, int> _requiredItems = new dictionary<string, int>();
dictionary<string, int> _collectedItems = new dictionary<string, int>();

_requiredItems.Add("Cube", 2); //2 being the number of Cubes required
_collectedItems.Add("Cube", 0); //0 meaning we don't have any collected
_requiredItems.Add("Sphere", 3); //3 being the number of Spheres required
_collectedItems.Add("Sphere", 0); //0 meaning we don't have any collected     

//stuff happens here
int _itemCollected = _itemsCollected["Cube"];
_itemsCollected["Cube"] = _itemCollected++;

所以现在有一种比较它们的方法。如果 _collectedItems 值 = _requiredItems 值,则返回 true,如果不是 false。

4

2 回答 2

3
HashSet<KeyValuePair<string, int>>.CreateSetComparer().Compare(
    new HashSet<KeyValuePair<string, int>>(dict1),
    new HashSet<KeyValuePair<string, int>>(dict1)
);
于 2013-10-21T00:34:54.290 回答
1

您可以使用 LINQAll方法,例如:

var dict1 = new Dictionary<string, int>();
var dict2 = new Dictionary<string, int>();

var result = dict1.All(k => k.Value == dict2[k.Key]);
于 2013-10-21T00:29:21.937 回答