Reputation: 33
I have this transitionMap: Dictionary<Dictionary<int, char>, int> transitionMap;
And I also have this dictionary: Dictionary<int, char> dictionary = new Dictionary<int, char> {{0, 'a'}};
.
How do I get the value inside transitionMap
that corresponds to dictionary1
.
This is what I've tried:
Dictionary<int, char> dictionary = new Dictionary<int, char> {{stateNum, symbol}};
if (this.transitionMap.ContainsKey(dictionary))
{
int nextState = this.transitionMap[dictionary];
Console.WriteLine("nextstate {0}", nextState);
}
Upvotes: 0
Views: 623
Reputation: 7590
Solution with Linq :
class Program
{
static void Main(string[] args)
{
var transitionMap = new Dictionary<Dictionary<int, char>, int>();
var dictionary = new Dictionary<int, char> { { stateNum, symbol } };
...
var found = transitionMap.Keys.FirstOrDefault(d => Equal(d, dictionary));
if(found == null)
{
int nextState = transitionMap[found];
Console.WriteLine("nextstate {0}", nextState);
}
}
static bool Equal(Dictionary<int, char> a, Dictionary<int, char> b)
{
return a.Count == b.Count && a.All(akv => b.TryGetValue(akv.Key, out char bval) && akv.Value == bval);
}
}
Upvotes: 1
Reputation: 16761
If you want to compare two dictionaries for equality then having a "super dictionary" will not help you in any way. You will have to manually compare all elements in two dictionaries.
First, compare the number of elements. Then run a loop through one dictionary, and with the key find the value in second dictionary, and if exists compare two values.
Upvotes: 2