I try to answer the above as follows:
In order to invert a dictionary, we need to do the following:
- Identify the set of (the original) dictionary values that shall become keys in the inverse dictionary.
- Create groups of keys from the original dictionary partitioned by the values from the previous step. Later we would generate a list of elements (original keys) from each group in order to make it a value in the inverse dictionary.
- Generate the new (inverse) dictionary by merging information from the two previous steps.
On condition the LINQ expression in the question does these 3 steps, the LINQ rewrite is correct and fully replaces the "ugly" 20 lines' method I had started with.
There remains one more question:
Is the LINQ compaction worth doing in this case? It might darken the purpose of the method rather than illuminate it.