Suppose there is data
Dictionary<string, HashSet<string>> rights = new Dictionary<string, HashSet<string>>() { {"Default", new HashSet<string>(){"Test1"}}, {"Vip", new HashSet<string>(){"Test2"}}, {"Admins", new HashSet<string>(){"Test3"}}, {"FullAdmins", new HashSet<string>(){"Test4"}}, }; string[] groups = new[] { "Default", "Vip", "Admins", "FullAdmins" };
Then with brute force groups everything will be simple
bool HasRight(string groupName, string rightName) { var ind = Array.IndexOf(groups, groupName); if (ind < 0) return false; for(int i = ind; i>=0; i--) if (rights[groups[i]].Contains(rightName)) return true; return false; }
Pts just check
Console.WriteLine(HasRight("Admins", "Test1")); // true Console.WriteLine(HasRight("Admins", "Test2")); // true Console.WriteLine(HasRight("Admins", "Test3")); // true Console.WriteLine(HasRight("Admins", "Test4")); // false
But you need to eat without going through all the elements, then on the basis of the existing dictionary you can assemble a new
var rights2 = new Dictionary<string, HashSet<string>>(); for (int i = 0; i < groups.Length; i++) { rights2.Add(groups[i], new HashSet<string>()); for (int j = i; j >= 0; j--) { foreach(var right in rights[groups[j]]) rights2[groups[i]].Add(right); } }
Then the rights check will look even easier.
bool HasRight2(string groupName, string rightName, Dictionary<string, HashSet<string>> rights2) { if (!rights2.ContainsKey(groupName)) return false; return rights2[groupName].Contains(rightName); }
Test:
Console.WriteLine(HasRight2("Admins", "Test1", rights2)); // true Console.WriteLine(HasRight2("Admins", "Test2", rights2)); // true Console.WriteLine(HasRight2("Admins", "Test3", rights2)); // true Console.WriteLine(HasRight2("Admins", "Test4", rights2)); // false
But in general, I would not recommend collecting new dictionaries, since when the source dictionary changes, the derivative will need to be recompiled.