Last active
August 29, 2015 14:06
-
-
Save ckpearson/9ce181670c2f37846fd7 to your computer and use it in GitHub Desktop.
Hierarchise a collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Takes an enumerable of items and builds a hierarchy from two properties present on the item | |
/// </summary> | |
/// <typeparam name="TOuter">The type of the outer key</typeparam> | |
/// <typeparam name="TInner">The type of the inner key</typeparam> | |
/// <typeparam name="TValue">The type of the item being processed</typeparam> | |
/// <param name="items">The enumerable to process</param> | |
/// <param name="outerSelector">Function for accessing the outer key</param> | |
/// <param name="innerSelector">Function for accessing the inner key</param> | |
public static Dictionary<TOuter, Dictionary<TInner, TValue>> Hierarchise<TOuter, TInner, TValue>(this IEnumerable<TValue> items, | |
Func<TValue, TOuter> outerSelector, | |
Func<TValue, TInner> innerSelector) | |
{ | |
return items.GroupBy(outerSelector) | |
.Select(og => new KeyValuePair<TOuter, IEnumerable<KeyValuePair<TInner, TValue>>>( | |
og.Key, og.Select(gi => new KeyValuePair<TInner, TValue>(innerSelector(gi), gi)))) | |
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToDictionary(k => k.Key, k => k.Value)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using it is as simple as:
Customers.Hierarchise(c => c.LastName, c => c.FirstName)
Just watch out for colliding keys as they're not dealt with.