Reputation: 11389
I have 2 collections in play here:
Dictionary<int, Privilege>
that acts a cacheDictionary<Workflow, List<enumPrivilege>>
that represents the permissions a given user has on every existem workflow.the thing here is that the cache has the full version of the privilege (along with the id, a friendly description, a text explanation, etc...) and I need to convert the Dictionary<Workflow, List<enumPrivilege>>
to Dictionary<Workflow, List<Privilege>>
...
Can I do that using a single lambda?
Upvotes: 1
Views: 1012
Reputation: 7034
Depends if you have logic that transforms a single enumeration value to the respective object, e.g. a factory.
In order to transform one Dictionary<K, List<V>>
you can use the ToDictionary()
method which accepts the KeyValuePair
two times, so you can use it first time for the key and second time for the value
Let's say we have
Dictionary<string, List<string>> a = new Dictionary<string, List<string>>();
And want to transform it to Dictionary b
which is string, List<int>
Dictionary<string, List<int>> b
= a.ToDictionary(
kvp => kvp.Key,
kvp => kvp.Value.Select(int.Parse).ToList()
);
The same way you should be able to turn the enumeration to object, by mapping (Select
) each enum value and execute the factory upon it
Dictionary<Workflow, List<Privilege>> b
= a.ToDictionary(
kvp => kvp.Key,
kvp => kvp.Value.Select(PrivilegeFactory.Create).ToList()
);
Or if the dictionary's key int
is the enumPrivile
value you can do
Dictionary<Workflow, List<Privilege>> b
= a.ToDictionary(
kvp => kvp.Key,
kvp => kvp.Value.Select(t => cacheDictionary[(int)t])
);
Upvotes: 4