Extension method for inverting a dictionary

This commit is contained in:
Chris Cameron
2018-02-22 15:06:57 -05:00
parent d4b0fc76cc
commit b0aa48803b

View File

@@ -386,5 +386,21 @@ namespace ICD.Common.Utils.Extensions
return extends.OrderByKey().Select(kvp => kvp.Value);
}
/// <summary>
/// Returns an inverse mapping of TValue -> TKey.
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="extends"></param>
/// <returns></returns>
[PublicAPI]
public static Dictionary<TValue, TKey> ToInverse<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> extends)
{
if (extends == null)
throw new ArgumentNullException("extends");
return extends.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
}
}
}