Skip to content

Instantly share code, notes, and snippets.

@jamesmanning
Last active December 8, 2016 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmanning/0a6cf6d0ca0546084adf5db657de8d2e to your computer and use it in GitHub Desktop.
Save jamesmanning/0a6cf6d0ca0546084adf5db657de8d2e to your computer and use it in GitHub Desktop.
public static class Extensions
{
public static Dictionary<TKeyOuter, Dictionary<TKeyMiddle, Dictionary<TKeyInner, TValue>>> ToThreeLevelDictionary<TKeyOuter, TKeyMiddle, TKeyInner, TValue, TInput>(
this IEnumerable<TInput> input,
Func<TInput, TKeyOuter> outerKeySelector,
Func<TInput, TKeyMiddle> middleKeySelector,
Func<TInput, TKeyInner> innerKeySelector,
Func<TInput, TValue> valueSelector)
{
return input
.ToLookup(outerKeySelector)
.ToDictionaryWithDuplicateKeyCheck(
x => x.Key,
x => x.ToTwoLevelDictionary(middleKeySelector, innerKeySelector, valueSelector));
}
public static Dictionary<TKeyOuter, Dictionary<TKeyInner, TValue>> ToTwoLevelDictionary<TKeyOuter, TKeyInner, TValue, TInput>(
this IEnumerable<TInput> input,
Func<TInput, TKeyOuter> outerKeySelector,
Func<TInput, TKeyInner> innerKeySelector,
Func<TInput, TValue> valueSelector)
{
return input
.ToLookup(outerKeySelector)
.ToDictionaryWithDuplicateKeyCheck(
x => x.Key,
x => x.ToDictionaryWithDuplicateKeyCheck(
innerKeySelector,
valueSelector));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment