Skip to content

Instantly share code, notes, and snippets.

@cameronsjo
Created May 5, 2015 21:00
Show Gist options
  • Select an option

  • Save cameronsjo/5963e1fe7a30bea1406f to your computer and use it in GitHub Desktop.

Select an option

Save cameronsjo/5963e1fe7a30bea1406f to your computer and use it in GitHub Desktop.
public static Dictionary<TKey, TValue> ToReverseDictionary<TModel, TKey, TValue>(this IEnumerable<TModel> collection,
Func<TModel, IEnumerable<TKey>> keySelector,
Func<TModel, TValue> valueSelector)
{
var dictionary = new Dictionary<TKey,TValue>();
foreach (var item in collection)
{
var value = valueSelector.Invoke(item);
foreach (var key in keySelector.Invoke(item))
dictionary.Add(key, value);
}
return dictionary;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment