IDictionaryExtensions.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. namespace Masuit.Tools
  5. {
  6. public static class IDictionaryExtensions
  7. {
  8. /// <summary>
  9. /// 添加或更新键值对
  10. /// </summary>
  11. /// <typeparam name="TKey"></typeparam>
  12. /// <typeparam name="TValue"></typeparam>
  13. /// <param name="this"></param>
  14. /// <param name="key">键</param>
  15. /// <param name="value">值</param>
  16. /// <returns></returns>
  17. public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue value)
  18. {
  19. if ([email protected](key))
  20. {
  21. @this.Add(new KeyValuePair<TKey, TValue>(key, value));
  22. }
  23. else
  24. {
  25. @this[key] = value;
  26. }
  27. return @this[key];
  28. }
  29. /// <summary>
  30. /// 添加或更新键值对
  31. /// </summary>
  32. /// <typeparam name="TKey"></typeparam>
  33. /// <typeparam name="TValue"></typeparam>
  34. /// <param name="this"></param>
  35. /// <param name="that">另一个字典集</param>
  36. /// <returns></returns>
  37. public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
  38. {
  39. foreach (var item in that)
  40. {
  41. AddOrUpdate(@this, item.Key, item.Value);
  42. }
  43. }
  44. /// <summary>
  45. /// 添加或更新键值对
  46. /// </summary>
  47. /// <typeparam name="TKey"></typeparam>
  48. /// <typeparam name="TValue"></typeparam>
  49. /// <param name="this"></param>
  50. /// <param name="that">另一个字典集</param>
  51. /// <returns></returns>
  52. public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
  53. {
  54. foreach (var item in @this)
  55. {
  56. AddOrUpdate(that, item.Key, item.Value);
  57. }
  58. }
  59. /// <summary>
  60. /// 添加或更新键值对
  61. /// </summary>
  62. /// <typeparam name="TKey"></typeparam>
  63. /// <typeparam name="TValue"></typeparam>
  64. /// <param name="this"></param>
  65. /// <param name="key">键</param>
  66. /// <param name="addValue">添加时的值</param>
  67. /// <param name="updateValueFactory">更新时的操作</param>
  68. /// <returns></returns>
  69. public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
  70. {
  71. if ([email protected](key))
  72. {
  73. @this.Add(new KeyValuePair<TKey, TValue>(key, addValue));
  74. }
  75. else
  76. {
  77. @this[key] = updateValueFactory(key, @this[key]);
  78. }
  79. return @this[key];
  80. }
  81. /// <summary>
  82. /// 添加或更新键值对
  83. /// </summary>
  84. /// <typeparam name="TKey"></typeparam>
  85. /// <typeparam name="TValue"></typeparam>
  86. /// <param name="this"></param>
  87. /// <param name="key">键</param>
  88. /// <param name="addValue">添加时的值</param>
  89. /// <param name="updateValue">更新时的值</param>
  90. /// <returns></returns>
  91. public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
  92. {
  93. if ([email protected](key))
  94. {
  95. @this.Add(new KeyValuePair<TKey, TValue>(key, addValue));
  96. }
  97. else
  98. {
  99. @this[key] = updateValue;
  100. }
  101. return @this[key];
  102. }
  103. /// <summary>
  104. /// 添加或更新键值对
  105. /// </summary>
  106. /// <typeparam name="TKey"></typeparam>
  107. /// <typeparam name="TValue"></typeparam>
  108. /// <param name="this"></param>
  109. /// <param name="that">另一个字典集</param>
  110. /// <param name="updateValueFactory">更新时的操作</param>
  111. /// <returns></returns>
  112. public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
  113. {
  114. foreach (var item in that)
  115. {
  116. AddOrUpdate(@this, item.Key, item.Value, updateValueFactory);
  117. }
  118. }
  119. /// <summary>
  120. /// 添加或更新键值对
  121. /// </summary>
  122. /// <typeparam name="TKey"></typeparam>
  123. /// <typeparam name="TValue"></typeparam>
  124. /// <param name="this"></param>
  125. /// <param name="that">另一个字典集</param>
  126. /// <param name="updateValueFactory">更新时的操作</param>
  127. /// <returns></returns>
  128. public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
  129. {
  130. foreach (var item in @this)
  131. {
  132. AddOrUpdate(that, item.Key, item.Value, updateValueFactory);
  133. }
  134. }
  135. /// <summary>
  136. /// 添加或更新键值对
  137. /// </summary>
  138. /// <typeparam name="TKey"></typeparam>
  139. /// <typeparam name="TValue"></typeparam>
  140. /// <param name="this"></param>
  141. /// <param name="key">键</param>
  142. /// <param name="addValueFactory">添加时的操作</param>
  143. /// <param name="updateValueFactory">更新时的操作</param>
  144. /// <returns></returns>
  145. public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
  146. {
  147. if ([email protected](key))
  148. {
  149. @this.Add(new KeyValuePair<TKey, TValue>(key, addValueFactory(key)));
  150. }
  151. else
  152. {
  153. @this[key] = updateValueFactory(key, @this[key]);
  154. }
  155. return @this[key];
  156. }
  157. /// <summary>
  158. /// 遍历IEnumerable
  159. /// </summary>
  160. /// <param name="dic"></param>
  161. /// <param name="action">回调方法</param>
  162. public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dic, Action<TKey, TValue> action)
  163. {
  164. foreach (var item in dic)
  165. {
  166. action(item.Key, item.Value);
  167. }
  168. }
  169. /// <summary>
  170. /// 安全的转换成字典集
  171. /// </summary>
  172. /// <typeparam name="TSource"></typeparam>
  173. /// <typeparam name="TKey"></typeparam>
  174. /// <param name="source"></param>
  175. /// <param name="keySelector">键选择器</param>
  176. /// <returns></returns>
  177. public static Dictionary<TKey, TSource> ToDictionarySafety<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  178. {
  179. var dic = new Dictionary<TKey, TSource>();
  180. foreach (var item in source)
  181. {
  182. AddOrUpdate(dic, keySelector(item), item);
  183. }
  184. return dic;
  185. }
  186. /// <summary>
  187. /// 安全的转换成字典集
  188. /// </summary>
  189. /// <typeparam name="TSource"></typeparam>
  190. /// <typeparam name="TKey"></typeparam>
  191. /// <typeparam name="TElement"></typeparam>
  192. /// <param name="source"></param>
  193. /// <param name="keySelector">键选择器</param>
  194. /// <param name="elementSelector">值选择器</param>
  195. /// <returns></returns>
  196. public static Dictionary<TKey, TElement> ToDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  197. {
  198. var dic = new Dictionary<TKey, TElement>();
  199. foreach (var item in source)
  200. {
  201. AddOrUpdate(dic, keySelector(item), elementSelector(item));
  202. }
  203. return dic;
  204. }
  205. /// <summary>
  206. /// 安全的转换成字典集
  207. /// </summary>
  208. /// <typeparam name="TSource"></typeparam>
  209. /// <typeparam name="TKey"></typeparam>
  210. /// <typeparam name="TElement"></typeparam>
  211. /// <param name="source"></param>
  212. /// <param name="keySelector">键选择器</param>
  213. /// <returns></returns>
  214. public static ConcurrentDictionary<TKey, TSource> ToConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  215. {
  216. var dic = new ConcurrentDictionary<TKey, TSource>();
  217. foreach (var item in source)
  218. {
  219. AddOrUpdate(dic, keySelector(item), item);
  220. }
  221. return dic;
  222. }
  223. /// <summary>
  224. /// 安全的转换成字典集
  225. /// </summary>
  226. /// <typeparam name="TSource"></typeparam>
  227. /// <typeparam name="TKey"></typeparam>
  228. /// <typeparam name="TElement"></typeparam>
  229. /// <param name="source"></param>
  230. /// <param name="keySelector">键选择器</param>
  231. /// <param name="elementSelector">值选择器</param>
  232. /// <returns></returns>
  233. public static ConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  234. {
  235. var dic = new ConcurrentDictionary<TKey, TElement>();
  236. foreach (var item in source)
  237. {
  238. AddOrUpdate(dic, keySelector(item), elementSelector(item));
  239. }
  240. return dic;
  241. }
  242. /// <summary>
  243. /// 转换成并发字典集合
  244. /// </summary>
  245. /// <typeparam name="TKey"></typeparam>
  246. /// <typeparam name="TValue"></typeparam>
  247. /// <param name="dic"></param>
  248. /// <returns></returns>
  249. public static ConcurrentDictionary<TKey, TValue> AsConcurrentDictionary<TKey, TValue>(this Dictionary<TKey, TValue> dic)
  250. {
  251. var cd = new ConcurrentDictionary<TKey, TValue>();
  252. cd.AddOrUpdate(dic);
  253. return cd;
  254. }
  255. /// <summary>
  256. /// 转换成普通字典集合
  257. /// </summary>
  258. /// <typeparam name="TKey"></typeparam>
  259. /// <typeparam name="TValue"></typeparam>
  260. /// <param name="dic"></param>
  261. /// <returns></returns>
  262. public static Dictionary<TKey, TValue> AsDictionary<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dic)
  263. {
  264. var cd = new Dictionary<TKey, TValue>();
  265. cd.AddOrUpdate(dic);
  266. return cd;
  267. }
  268. }
  269. }