-1

2 つの辞書を比較する最良の方法は何ですか? その中の項目は、常に同じ順序である必要があります。

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 値の場合、false でない場合は true を返します。

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 に答える