|
@@ -12,11 +12,8 @@ namespace Masuit.Tools
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
- /// <returns></returns>
|
|
|
|
public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
{
|
|
{
|
|
foreach (var item in that)
|
|
foreach (var item in that)
|
|
@@ -28,11 +25,8 @@ namespace Masuit.Tools
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
- /// <returns></returns>
|
|
|
|
public static void AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
public static void AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
{
|
|
{
|
|
foreach (var item in that)
|
|
foreach (var item in that)
|
|
@@ -44,11 +38,8 @@ namespace Masuit.Tools
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
- /// <returns></returns>
|
|
|
|
public static void AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
public static void AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
{
|
|
{
|
|
foreach (var item in that)
|
|
foreach (var item in that)
|
|
@@ -60,11 +51,8 @@ namespace Masuit.Tools
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
- /// <returns></returns>
|
|
|
|
public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
{
|
|
{
|
|
foreach (var item in @this)
|
|
foreach (var item in @this)
|
|
@@ -76,11 +64,8 @@ namespace Masuit.Tools
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
- /// <returns></returns>
|
|
|
|
public static void AddOrUpdateTo<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
public static void AddOrUpdateTo<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
{
|
|
{
|
|
foreach (var item in @this)
|
|
foreach (var item in @this)
|
|
@@ -92,11 +77,8 @@ namespace Masuit.Tools
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
- /// <returns></returns>
|
|
|
|
public static void AddOrUpdateTo<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
public static void AddOrUpdateTo<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that)
|
|
{
|
|
{
|
|
foreach (var item in @this)
|
|
foreach (var item in @this)
|
|
@@ -105,23 +87,18 @@ namespace Masuit.Tools
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#if NETSTANDARD2_1_OR_GREATER
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
|
|
public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
|
|
{
|
|
{
|
|
- if ([email protected](key))
|
|
|
|
- {
|
|
|
|
- @this.Add(key, addValue);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
|
|
+ if ([email protected](key, addValue))
|
|
{
|
|
{
|
|
@this[key] = updateValueFactory(key, @this[key]);
|
|
@this[key] = updateValueFactory(key, @this[key]);
|
|
}
|
|
}
|
|
@@ -132,20 +109,13 @@ namespace Masuit.Tools
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
|
|
public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
|
|
{
|
|
{
|
|
- if ([email protected](key))
|
|
|
|
- {
|
|
|
|
- @this.Add(key, addValue);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
|
|
+ if ([email protected](key, addValue))
|
|
{
|
|
{
|
|
@this[key] = updateValueFactory(key, @this[key]);
|
|
@this[key] = updateValueFactory(key, @this[key]);
|
|
}
|
|
}
|
|
@@ -153,6 +123,8 @@ namespace Masuit.Tools
|
|
return @this[key];
|
|
return @this[key];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#endif
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -162,14 +134,9 @@ namespace Masuit.Tools
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
|
|
public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactory)
|
|
{
|
|
{
|
|
- if ([email protected](key))
|
|
|
|
- {
|
|
|
|
- @this.TryAdd(key, addValue);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
|
|
+ if ([email protected](key, addValue))
|
|
{
|
|
{
|
|
@this[key] = updateValueFactory(key, @this[key]);
|
|
@this[key] = updateValueFactory(key, @this[key]);
|
|
}
|
|
}
|
|
@@ -177,6 +144,8 @@ namespace Masuit.Tools
|
|
return @this[key];
|
|
return @this[key];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#if NETSTANDARD2_1_OR_GREATER
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -186,14 +155,9 @@ namespace Masuit.Tools
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
{
|
|
{
|
|
- if ([email protected](key))
|
|
|
|
- {
|
|
|
|
- @this.Add(key, addValue);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
|
|
+ if ([email protected](key, addValue))
|
|
{
|
|
{
|
|
@this[key] = await updateValueFactory(key, @this[key]);
|
|
@this[key] = await updateValueFactory(key, @this[key]);
|
|
}
|
|
}
|
|
@@ -210,14 +174,9 @@ namespace Masuit.Tools
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
{
|
|
{
|
|
- if ([email protected](key))
|
|
|
|
- {
|
|
|
|
- @this.Add(key, addValue);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
|
|
+ if ([email protected](key, addValue))
|
|
{
|
|
{
|
|
@this[key] = await updateValueFactory(key, @this[key]);
|
|
@this[key] = await updateValueFactory(key, @this[key]);
|
|
}
|
|
}
|
|
@@ -225,6 +184,8 @@ namespace Masuit.Tools
|
|
return @this[key];
|
|
return @this[key];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#endif
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -234,14 +195,9 @@ namespace Masuit.Tools
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
{
|
|
{
|
|
- if ([email protected](key))
|
|
|
|
- {
|
|
|
|
- @this.TryAdd(key, addValue);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
|
|
+ if ([email protected](key, addValue))
|
|
{
|
|
{
|
|
@this[key] = await updateValueFactory(key, @this[key]);
|
|
@this[key] = await updateValueFactory(key, @this[key]);
|
|
}
|
|
}
|
|
@@ -249,6 +205,8 @@ namespace Masuit.Tools
|
|
return @this[key];
|
|
return @this[key];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#if NETSTANDARD2_1_OR_GREATER
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -258,14 +216,9 @@ namespace Masuit.Tools
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="updateValue">更新时的值</param>
|
|
/// <param name="updateValue">更新时的值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
|
|
public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
|
|
{
|
|
{
|
|
- if ([email protected](key))
|
|
|
|
- {
|
|
|
|
- @this.Add(key, addValue);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
|
|
+ if ([email protected](key, addValue))
|
|
{
|
|
{
|
|
@this[key] = updateValue;
|
|
@this[key] = updateValue;
|
|
}
|
|
}
|
|
@@ -276,20 +229,13 @@ namespace Masuit.Tools
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="updateValue">更新时的值</param>
|
|
/// <param name="updateValue">更新时的值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
|
|
public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
|
|
{
|
|
{
|
|
- if ([email protected](key))
|
|
|
|
- {
|
|
|
|
- @this.Add(key, addValue);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
|
|
+ if ([email protected](key, addValue))
|
|
{
|
|
{
|
|
@this[key] = updateValue;
|
|
@this[key] = updateValue;
|
|
}
|
|
}
|
|
@@ -297,6 +243,8 @@ namespace Masuit.Tools
|
|
return @this[key];
|
|
return @this[key];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#endif
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -306,14 +254,9 @@ namespace Masuit.Tools
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="addValue">添加时的值</param>
|
|
/// <param name="updateValue">更新时的值</param>
|
|
/// <param name="updateValue">更新时的值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
|
|
public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, TValue addValue, TValue updateValue)
|
|
{
|
|
{
|
|
- if ([email protected](key))
|
|
|
|
- {
|
|
|
|
- @this.TryAdd(key, addValue);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
|
|
+ if ([email protected](key, addValue))
|
|
{
|
|
{
|
|
@this[key] = updateValue;
|
|
@this[key] = updateValue;
|
|
}
|
|
}
|
|
@@ -321,6 +264,8 @@ namespace Masuit.Tools
|
|
return @this[key];
|
|
return @this[key];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#if NETSTANDARD2_1_OR_GREATER
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -329,7 +274,6 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
{
|
|
{
|
|
foreach (var item in that)
|
|
foreach (var item in that)
|
|
@@ -346,7 +290,6 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static void AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
public static void AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
{
|
|
{
|
|
foreach (var item in that)
|
|
foreach (var item in that)
|
|
@@ -355,6 +298,8 @@ namespace Masuit.Tools
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#endif
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -363,7 +308,6 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static void AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
public static void AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
{
|
|
{
|
|
foreach (var item in that)
|
|
foreach (var item in that)
|
|
@@ -372,6 +316,8 @@ namespace Masuit.Tools
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#if NETSTANDARD2_1_OR_GREATER
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -380,7 +326,6 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static Task AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
public static Task AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
{
|
|
{
|
|
return that.ForeachAsync(item => AddOrUpdateAsync(@this, item.Key, item.Value, updateValueFactory));
|
|
return that.ForeachAsync(item => AddOrUpdateAsync(@this, item.Key, item.Value, updateValueFactory));
|
|
@@ -394,12 +339,13 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static Task AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
public static Task AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
{
|
|
{
|
|
return that.ForeachAsync(item => AddOrUpdateAsync(@this, item.Key, item.Value, updateValueFactory));
|
|
return that.ForeachAsync(item => AddOrUpdateAsync(@this, item.Key, item.Value, updateValueFactory));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#endif
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -408,12 +354,13 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static Task AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
public static Task AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
{
|
|
{
|
|
return that.ForeachAsync(item => AddOrUpdateAsync(@this, item.Key, item.Value, updateValueFactory));
|
|
return that.ForeachAsync(item => AddOrUpdateAsync(@this, item.Key, item.Value, updateValueFactory));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#if NETSTANDARD2_1_OR_GREATER
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -422,7 +369,6 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
{
|
|
{
|
|
foreach (var item in @this)
|
|
foreach (var item in @this)
|
|
@@ -439,7 +385,6 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
{
|
|
{
|
|
foreach (var item in @this)
|
|
foreach (var item in @this)
|
|
@@ -448,6 +393,8 @@ namespace Masuit.Tools
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#endif
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -456,7 +403,6 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableConcurrentDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
public static void AddOrUpdateTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableConcurrentDictionary<TKey, TValue> that, Func<TKey, TValue, TValue> updateValueFactory)
|
|
{
|
|
{
|
|
foreach (var item in @this)
|
|
foreach (var item in @this)
|
|
@@ -465,6 +411,8 @@ namespace Masuit.Tools
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#if NETSTANDARD2_1_OR_GREATER
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -473,7 +421,6 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, IDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
{
|
|
{
|
|
return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
|
|
return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
|
|
@@ -487,12 +434,13 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
{
|
|
{
|
|
return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
|
|
return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#endif
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -501,7 +449,6 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="that">另一个字典集</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableConcurrentDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
public static Task AddOrUpdateAsyncTo<TKey, TValue>(this IDictionary<TKey, TValue> @this, NullableConcurrentDictionary<TKey, TValue> that, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
{
|
|
{
|
|
return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
|
|
return @this.ForeachAsync(item => AddOrUpdateAsync(that, item.Key, item.Value, updateValueFactory));
|
|
@@ -516,7 +463,6 @@ namespace Masuit.Tools
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValueFactory">添加时的操作</param>
|
|
/// <param name="addValueFactory">添加时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
|
|
public static TValue AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
|
|
{
|
|
{
|
|
if ([email protected](key))
|
|
if ([email protected](key))
|
|
@@ -540,7 +486,6 @@ namespace Masuit.Tools
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValueFactory">添加时的操作</param>
|
|
/// <param name="addValueFactory">添加时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
|
|
public static TValue AddOrUpdate<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
|
|
{
|
|
{
|
|
if ([email protected](key))
|
|
if ([email protected](key))
|
|
@@ -564,7 +509,6 @@ namespace Masuit.Tools
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValueFactory">添加时的操作</param>
|
|
/// <param name="addValueFactory">添加时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
|
|
public static TValue AddOrUpdate<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
|
|
{
|
|
{
|
|
if ([email protected](key))
|
|
if ([email protected](key))
|
|
@@ -588,7 +532,6 @@ namespace Masuit.Tools
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValueFactory">添加时的操作</param>
|
|
/// <param name="addValueFactory">添加时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
{
|
|
{
|
|
if ([email protected](key))
|
|
if ([email protected](key))
|
|
@@ -603,6 +546,8 @@ namespace Masuit.Tools
|
|
return @this[key];
|
|
return @this[key];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#if NETSTANDARD2_1_OR_GREATER
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -612,14 +557,9 @@ namespace Masuit.Tools
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValueFactory">添加时的操作</param>
|
|
/// <param name="addValueFactory">添加时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
{
|
|
{
|
|
- if ([email protected](key))
|
|
|
|
- {
|
|
|
|
- @this.Add(key, await addValueFactory(key));
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
|
|
+ if ([email protected](key, await addValueFactory(key)))
|
|
{
|
|
{
|
|
@this[key] = await updateValueFactory(key, @this[key]);
|
|
@this[key] = await updateValueFactory(key, @this[key]);
|
|
}
|
|
}
|
|
@@ -627,6 +567,8 @@ namespace Masuit.Tools
|
|
return @this[key];
|
|
return @this[key];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#endif
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 添加或更新键值对
|
|
/// 添加或更新键值对
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -636,14 +578,9 @@ namespace Masuit.Tools
|
|
/// <param name="key">键</param>
|
|
/// <param name="key">键</param>
|
|
/// <param name="addValueFactory">添加时的操作</param>
|
|
/// <param name="addValueFactory">添加时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
/// <param name="updateValueFactory">更新时的操作</param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
public static async Task<TValue> AddOrUpdateAsync<TKey, TValue>(this NullableConcurrentDictionary<TKey, TValue> @this, TKey key, Func<TKey, Task<TValue>> addValueFactory, Func<TKey, TValue, Task<TValue>> updateValueFactory)
|
|
{
|
|
{
|
|
- if ([email protected](key))
|
|
|
|
- {
|
|
|
|
- @this.TryAdd(key, await addValueFactory(key));
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
|
|
+ if ([email protected](key, await addValueFactory(key)))
|
|
{
|
|
{
|
|
@this[key] = await updateValueFactory(key, @this[key]);
|
|
@this[key] = await updateValueFactory(key, @this[key]);
|
|
}
|
|
}
|
|
@@ -659,7 +596,6 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="addValueFactory"></param>
|
|
/// <param name="addValueFactory"></param>
|
|
- /// <returns></returns>
|
|
|
|
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TValue> addValueFactory)
|
|
public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<TValue> addValueFactory)
|
|
{
|
|
{
|
|
if ([email protected](key))
|
|
if ([email protected](key))
|
|
@@ -678,7 +614,6 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="addValueFactory"></param>
|
|
/// <param name="addValueFactory"></param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<TValue> GetOrAddAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<Task<TValue>> addValueFactory)
|
|
public static async Task<TValue> GetOrAddAsync<TKey, TValue>(this IDictionary<TKey, TValue> @this, TKey key, Func<Task<TValue>> addValueFactory)
|
|
{
|
|
{
|
|
if ([email protected](key))
|
|
if ([email protected](key))
|
|
@@ -689,6 +624,8 @@ namespace Masuit.Tools
|
|
return @this[key];
|
|
return @this[key];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#if NETSTANDARD2_1_OR_GREATER
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 获取或添加
|
|
/// 获取或添加
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -697,18 +634,13 @@ namespace Masuit.Tools
|
|
/// <param name="this"></param>
|
|
/// <param name="this"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="addValue"></param>
|
|
/// <param name="addValue"></param>
|
|
- /// <returns></returns>
|
|
|
|
public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> @this, TKey key, TValue addValue)
|
|
public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> @this, TKey key, TValue addValue)
|
|
{
|
|
{
|
|
- if ([email protected](key))
|
|
|
|
- {
|
|
|
|
- @this[key] = addValue;
|
|
|
|
- return addValue;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return @this[key];
|
|
|
|
|
|
+ return @this.TryAdd(key, addValue) ? addValue : @this[key];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#endif
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 遍历IEnumerable
|
|
/// 遍历IEnumerable
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -739,11 +671,11 @@ namespace Masuit.Tools
|
|
/// <typeparam name="TKey"></typeparam>
|
|
/// <typeparam name="TKey"></typeparam>
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
- /// <returns></returns>
|
|
|
|
public static NullableDictionary<TKey, TSource> ToDictionarySafety<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
|
|
public static NullableDictionary<TKey, TSource> ToDictionarySafety<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
|
|
{
|
|
{
|
|
- var dic = new NullableDictionary<TKey, TSource>(source.Count());
|
|
|
|
- foreach (var item in source)
|
|
|
|
|
|
+ var items = source as IList<TSource> ?? source.ToList();
|
|
|
|
+ var dic = new NullableDictionary<TKey, TSource>(items.Count);
|
|
|
|
+ foreach (var item in items)
|
|
{
|
|
{
|
|
dic[keySelector(item)] = item;
|
|
dic[keySelector(item)] = item;
|
|
}
|
|
}
|
|
@@ -759,11 +691,14 @@ namespace Masuit.Tools
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static NullableDictionary<TKey, TSource> ToDictionarySafety<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TSource defaultValue)
|
|
public static NullableDictionary<TKey, TSource> ToDictionarySafety<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TSource defaultValue)
|
|
{
|
|
{
|
|
- var dic = new NullableDictionary<TKey, TSource>(source.Count()) { FallbackValue = defaultValue };
|
|
|
|
- foreach (var item in source)
|
|
|
|
|
|
+ var items = source as IList<TSource> ?? source.ToList();
|
|
|
|
+ var dic = new NullableDictionary<TKey, TSource>(items.Count)
|
|
|
|
+ {
|
|
|
|
+ FallbackValue = defaultValue
|
|
|
|
+ };
|
|
|
|
+ foreach (var item in items)
|
|
{
|
|
{
|
|
dic[keySelector(item)] = item;
|
|
dic[keySelector(item)] = item;
|
|
}
|
|
}
|
|
@@ -780,11 +715,11 @@ namespace Masuit.Tools
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
- /// <returns></returns>
|
|
|
|
public static NullableDictionary<TKey, TElement> ToDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
|
|
public static NullableDictionary<TKey, TElement> ToDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
|
|
{
|
|
{
|
|
- var dic = new NullableDictionary<TKey, TElement>(source.Count());
|
|
|
|
- foreach (var item in source)
|
|
|
|
|
|
+ var items = source as IList<TSource> ?? source.ToList();
|
|
|
|
+ var dic = new NullableDictionary<TKey, TElement>(items.Count);
|
|
|
|
+ foreach (var item in items)
|
|
{
|
|
{
|
|
dic[keySelector(item)] = elementSelector(item);
|
|
dic[keySelector(item)] = elementSelector(item);
|
|
}
|
|
}
|
|
@@ -802,11 +737,14 @@ namespace Masuit.Tools
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static NullableDictionary<TKey, TElement> ToDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, TElement defaultValue)
|
|
public static NullableDictionary<TKey, TElement> ToDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, TElement defaultValue)
|
|
{
|
|
{
|
|
- var dic = new NullableDictionary<TKey, TElement>(source.Count()) { FallbackValue = defaultValue };
|
|
|
|
- foreach (var item in source)
|
|
|
|
|
|
+ var items = source as IList<TSource> ?? source.ToList();
|
|
|
|
+ var dic = new NullableDictionary<TKey, TElement>(items.Count)
|
|
|
|
+ {
|
|
|
|
+ FallbackValue = defaultValue
|
|
|
|
+ };
|
|
|
|
+ foreach (var item in items)
|
|
{
|
|
{
|
|
dic[keySelector(item)] = elementSelector(item);
|
|
dic[keySelector(item)] = elementSelector(item);
|
|
}
|
|
}
|
|
@@ -823,11 +761,11 @@ namespace Masuit.Tools
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<NullableDictionary<TKey, TElement>> ToDictionarySafetyAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector)
|
|
public static async Task<NullableDictionary<TKey, TElement>> ToDictionarySafetyAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector)
|
|
{
|
|
{
|
|
- var dic = new NullableDictionary<TKey, TElement>(source.Count());
|
|
|
|
- await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
|
|
|
|
|
|
+ var items = source as IList<TSource> ?? source.ToList();
|
|
|
|
+ var dic = new NullableDictionary<TKey, TElement>(items.Count);
|
|
|
|
+ await items.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
|
|
return dic;
|
|
return dic;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -841,11 +779,14 @@ namespace Masuit.Tools
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<NullableDictionary<TKey, TElement>> ToDictionarySafetyAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector, TElement defaultValue)
|
|
public static async Task<NullableDictionary<TKey, TElement>> ToDictionarySafetyAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector, TElement defaultValue)
|
|
{
|
|
{
|
|
- var dic = new NullableDictionary<TKey, TElement>(source.Count()) { FallbackValue = defaultValue };
|
|
|
|
- await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
|
|
|
|
|
|
+ var items = source as IList<TSource> ?? source.ToList();
|
|
|
|
+ var dic = new NullableDictionary<TKey, TElement>(items.Count)
|
|
|
|
+ {
|
|
|
|
+ FallbackValue = defaultValue
|
|
|
|
+ };
|
|
|
|
+ await items.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
|
|
return dic;
|
|
return dic;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -856,11 +797,11 @@ namespace Masuit.Tools
|
|
/// <typeparam name="TKey"></typeparam>
|
|
/// <typeparam name="TKey"></typeparam>
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
- /// <returns></returns>
|
|
|
|
public static DisposableDictionary<TKey, TSource> ToDisposableDictionarySafety<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) where TSource : IDisposable
|
|
public static DisposableDictionary<TKey, TSource> ToDisposableDictionarySafety<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) where TSource : IDisposable
|
|
{
|
|
{
|
|
- var dic = new DisposableDictionary<TKey, TSource>(source.Count());
|
|
|
|
- foreach (var item in source)
|
|
|
|
|
|
+ var items = source as IList<TSource> ?? source.ToList();
|
|
|
|
+ var dic = new DisposableDictionary<TKey, TSource>(items.Count);
|
|
|
|
+ foreach (var item in items)
|
|
{
|
|
{
|
|
dic[keySelector(item)] = item;
|
|
dic[keySelector(item)] = item;
|
|
}
|
|
}
|
|
@@ -876,11 +817,14 @@ namespace Masuit.Tools
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static DisposableDictionary<TKey, TSource> ToDisposableDictionarySafety<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TSource defaultValue) where TSource : IDisposable
|
|
public static DisposableDictionary<TKey, TSource> ToDisposableDictionarySafety<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TSource defaultValue) where TSource : IDisposable
|
|
{
|
|
{
|
|
- var dic = new DisposableDictionary<TKey, TSource>(source.Count()) { FallbackValue = defaultValue };
|
|
|
|
- foreach (var item in source)
|
|
|
|
|
|
+ var items = source as IList<TSource> ?? source.ToList();
|
|
|
|
+ var dic = new DisposableDictionary<TKey, TSource>(items.Count)
|
|
|
|
+ {
|
|
|
|
+ FallbackValue = defaultValue
|
|
|
|
+ };
|
|
|
|
+ foreach (var item in items)
|
|
{
|
|
{
|
|
dic[keySelector(item)] = item;
|
|
dic[keySelector(item)] = item;
|
|
}
|
|
}
|
|
@@ -897,11 +841,11 @@ namespace Masuit.Tools
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
- /// <returns></returns>
|
|
|
|
public static DisposableDictionary<TKey, TElement> ToDisposableDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector) where TElement : IDisposable
|
|
public static DisposableDictionary<TKey, TElement> ToDisposableDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector) where TElement : IDisposable
|
|
{
|
|
{
|
|
- var dic = new DisposableDictionary<TKey, TElement>(source.Count());
|
|
|
|
- foreach (var item in source)
|
|
|
|
|
|
+ var items = source as IList<TSource> ?? source.ToList();
|
|
|
|
+ var dic = new DisposableDictionary<TKey, TElement>(items.Count);
|
|
|
|
+ foreach (var item in items)
|
|
{
|
|
{
|
|
dic[keySelector(item)] = elementSelector(item);
|
|
dic[keySelector(item)] = elementSelector(item);
|
|
}
|
|
}
|
|
@@ -919,11 +863,14 @@ namespace Masuit.Tools
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static DisposableDictionary<TKey, TElement> ToDisposableDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, TElement defaultValue) where TElement : IDisposable
|
|
public static DisposableDictionary<TKey, TElement> ToDisposableDictionarySafety<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, TElement defaultValue) where TElement : IDisposable
|
|
{
|
|
{
|
|
- var dic = new DisposableDictionary<TKey, TElement>(source.Count()) { FallbackValue = defaultValue };
|
|
|
|
- foreach (var item in source)
|
|
|
|
|
|
+ var items = source as IList<TSource> ?? source.ToList();
|
|
|
|
+ var dic = new DisposableDictionary<TKey, TElement>(items.Count)
|
|
|
|
+ {
|
|
|
|
+ FallbackValue = defaultValue
|
|
|
|
+ };
|
|
|
|
+ foreach (var item in items)
|
|
{
|
|
{
|
|
dic[keySelector(item)] = elementSelector(item);
|
|
dic[keySelector(item)] = elementSelector(item);
|
|
}
|
|
}
|
|
@@ -940,11 +887,11 @@ namespace Masuit.Tools
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<DisposableDictionary<TKey, TElement>> ToDisposableDictionarySafetyAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector) where TElement : IDisposable
|
|
public static async Task<DisposableDictionary<TKey, TElement>> ToDisposableDictionarySafetyAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector) where TElement : IDisposable
|
|
{
|
|
{
|
|
- var dic = new DisposableDictionary<TKey, TElement>(source.Count());
|
|
|
|
- await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
|
|
|
|
|
|
+ var items = source as IList<TSource> ?? source.ToList();
|
|
|
|
+ var dic = new DisposableDictionary<TKey, TElement>(items.Count);
|
|
|
|
+ await items.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
|
|
return dic;
|
|
return dic;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -958,11 +905,14 @@ namespace Masuit.Tools
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<DisposableDictionary<TKey, TElement>> ToDisposableDictionarySafetyAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector, TElement defaultValue) where TElement : IDisposable
|
|
public static async Task<DisposableDictionary<TKey, TElement>> ToDisposableDictionarySafetyAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector, TElement defaultValue) where TElement : IDisposable
|
|
{
|
|
{
|
|
- var dic = new DisposableDictionary<TKey, TElement>(source.Count()) { FallbackValue = defaultValue };
|
|
|
|
- await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
|
|
|
|
|
|
+ var items = source as IList<TSource> ?? source.ToList();
|
|
|
|
+ var dic = new DisposableDictionary<TKey, TElement>(items.Count)
|
|
|
|
+ {
|
|
|
|
+ FallbackValue = defaultValue
|
|
|
|
+ };
|
|
|
|
+ await items.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
|
|
return dic;
|
|
return dic;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -973,7 +923,6 @@ namespace Masuit.Tools
|
|
/// <typeparam name="TKey"></typeparam>
|
|
/// <typeparam name="TKey"></typeparam>
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
- /// <returns></returns>
|
|
|
|
public static NullableConcurrentDictionary<TKey, TSource> ToConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
|
|
public static NullableConcurrentDictionary<TKey, TSource> ToConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
|
|
{
|
|
{
|
|
var dic = new NullableConcurrentDictionary<TKey, TSource>();
|
|
var dic = new NullableConcurrentDictionary<TKey, TSource>();
|
|
@@ -993,10 +942,12 @@ namespace Masuit.Tools
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static NullableConcurrentDictionary<TKey, TSource> ToConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TSource defaultValue)
|
|
public static NullableConcurrentDictionary<TKey, TSource> ToConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TSource defaultValue)
|
|
{
|
|
{
|
|
- var dic = new NullableConcurrentDictionary<TKey, TSource>() { FallbackValue = defaultValue };
|
|
|
|
|
|
+ var dic = new NullableConcurrentDictionary<TKey, TSource>()
|
|
|
|
+ {
|
|
|
|
+ FallbackValue = defaultValue
|
|
|
|
+ };
|
|
foreach (var item in source)
|
|
foreach (var item in source)
|
|
{
|
|
{
|
|
dic[keySelector(item)] = item;
|
|
dic[keySelector(item)] = item;
|
|
@@ -1014,7 +965,6 @@ namespace Masuit.Tools
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
- /// <returns></returns>
|
|
|
|
public static NullableConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
|
|
public static NullableConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
|
|
{
|
|
{
|
|
var dic = new NullableConcurrentDictionary<TKey, TElement>();
|
|
var dic = new NullableConcurrentDictionary<TKey, TElement>();
|
|
@@ -1035,10 +985,13 @@ namespace Masuit.Tools
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
- /// <returns></returns>
|
|
|
|
|
|
+ /// <param name="defaultValue"></param>
|
|
public static NullableConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, TElement defaultValue)
|
|
public static NullableConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, TElement defaultValue)
|
|
{
|
|
{
|
|
- var dic = new NullableConcurrentDictionary<TKey, TElement>() { FallbackValue = defaultValue };
|
|
|
|
|
|
+ var dic = new NullableConcurrentDictionary<TKey, TElement>()
|
|
|
|
+ {
|
|
|
|
+ FallbackValue = defaultValue
|
|
|
|
+ };
|
|
foreach (var item in source)
|
|
foreach (var item in source)
|
|
{
|
|
{
|
|
dic[keySelector(item)] = elementSelector(item);
|
|
dic[keySelector(item)] = elementSelector(item);
|
|
@@ -1056,7 +1009,6 @@ namespace Masuit.Tools
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<NullableConcurrentDictionary<TKey, TElement>> ToConcurrentDictionaryAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector)
|
|
public static async Task<NullableConcurrentDictionary<TKey, TElement>> ToConcurrentDictionaryAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector)
|
|
{
|
|
{
|
|
var dic = new ConcurrentDictionary<TKey, TElement>();
|
|
var dic = new ConcurrentDictionary<TKey, TElement>();
|
|
@@ -1074,10 +1026,12 @@ namespace Masuit.Tools
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<NullableConcurrentDictionary<TKey, TElement>> ToConcurrentDictionaryAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector, TElement defaultValue)
|
|
public static async Task<NullableConcurrentDictionary<TKey, TElement>> ToConcurrentDictionaryAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector, TElement defaultValue)
|
|
{
|
|
{
|
|
- var dic = new NullableConcurrentDictionary<TKey, TElement>() { FallbackValue = defaultValue };
|
|
|
|
|
|
+ var dic = new NullableConcurrentDictionary<TKey, TElement>
|
|
|
|
+ {
|
|
|
|
+ FallbackValue = defaultValue
|
|
|
|
+ };
|
|
await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
|
|
await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
|
|
return dic;
|
|
return dic;
|
|
}
|
|
}
|
|
@@ -1089,7 +1043,6 @@ namespace Masuit.Tools
|
|
/// <typeparam name="TKey"></typeparam>
|
|
/// <typeparam name="TKey"></typeparam>
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
- /// <returns></returns>
|
|
|
|
public static DisposableConcurrentDictionary<TKey, TSource> ToDisposableConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) where TSource : IDisposable
|
|
public static DisposableConcurrentDictionary<TKey, TSource> ToDisposableConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) where TSource : IDisposable
|
|
{
|
|
{
|
|
var dic = new DisposableConcurrentDictionary<TKey, TSource>();
|
|
var dic = new DisposableConcurrentDictionary<TKey, TSource>();
|
|
@@ -1104,15 +1057,15 @@ namespace Masuit.Tools
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 安全的转换成字典集
|
|
/// 安全的转换成字典集
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TSource"></typeparam>
|
|
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static DisposableConcurrentDictionary<TKey, TSource> ToDisposableConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TSource defaultValue) where TSource : IDisposable
|
|
public static DisposableConcurrentDictionary<TKey, TSource> ToDisposableConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, TSource defaultValue) where TSource : IDisposable
|
|
{
|
|
{
|
|
- var dic = new DisposableConcurrentDictionary<TKey, TSource>() { FallbackValue = defaultValue };
|
|
|
|
|
|
+ var dic = new DisposableConcurrentDictionary<TKey, TSource>
|
|
|
|
+ {
|
|
|
|
+ FallbackValue = defaultValue
|
|
|
|
+ };
|
|
foreach (var item in source)
|
|
foreach (var item in source)
|
|
{
|
|
{
|
|
dic[keySelector(item)] = item;
|
|
dic[keySelector(item)] = item;
|
|
@@ -1124,13 +1077,9 @@ namespace Masuit.Tools
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 安全的转换成字典集
|
|
/// 安全的转换成字典集
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TSource"></typeparam>
|
|
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TElement"></typeparam>
|
|
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
- /// <returns></returns>
|
|
|
|
public static DisposableConcurrentDictionary<TKey, TElement> ToDisposableConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector) where TElement : IDisposable
|
|
public static DisposableConcurrentDictionary<TKey, TElement> ToDisposableConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector) where TElement : IDisposable
|
|
{
|
|
{
|
|
var dic = new DisposableConcurrentDictionary<TKey, TElement>();
|
|
var dic = new DisposableConcurrentDictionary<TKey, TElement>();
|
|
@@ -1145,16 +1094,16 @@ namespace Masuit.Tools
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 安全的转换成字典集
|
|
/// 安全的转换成字典集
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TSource"></typeparam>
|
|
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TElement"></typeparam>
|
|
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
- /// <returns></returns>
|
|
|
|
|
|
+ /// <param name="defaultValue"></param>
|
|
public static DisposableConcurrentDictionary<TKey, TElement> ToDisposableConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, TElement defaultValue) where TElement : IDisposable
|
|
public static DisposableConcurrentDictionary<TKey, TElement> ToDisposableConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, TElement defaultValue) where TElement : IDisposable
|
|
{
|
|
{
|
|
- var dic = new DisposableConcurrentDictionary<TKey, TElement>() { FallbackValue = defaultValue };
|
|
|
|
|
|
+ var dic = new DisposableConcurrentDictionary<TKey, TElement>()
|
|
|
|
+ {
|
|
|
|
+ FallbackValue = defaultValue
|
|
|
|
+ };
|
|
foreach (var item in source)
|
|
foreach (var item in source)
|
|
{
|
|
{
|
|
dic[keySelector(item)] = elementSelector(item);
|
|
dic[keySelector(item)] = elementSelector(item);
|
|
@@ -1166,13 +1115,9 @@ namespace Masuit.Tools
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 安全的转换成字典集
|
|
/// 安全的转换成字典集
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TSource"></typeparam>
|
|
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TElement"></typeparam>
|
|
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<DisposableConcurrentDictionary<TKey, TElement>> ToDisposableConcurrentDictionaryAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector) where TElement : IDisposable
|
|
public static async Task<DisposableConcurrentDictionary<TKey, TElement>> ToDisposableConcurrentDictionaryAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector) where TElement : IDisposable
|
|
{
|
|
{
|
|
var dic = new DisposableConcurrentDictionary<TKey, TElement>();
|
|
var dic = new DisposableConcurrentDictionary<TKey, TElement>();
|
|
@@ -1183,17 +1128,16 @@ namespace Masuit.Tools
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 安全的转换成字典集
|
|
/// 安全的转换成字典集
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TSource"></typeparam>
|
|
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TElement"></typeparam>
|
|
|
|
/// <param name="source"></param>
|
|
/// <param name="source"></param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="keySelector">键选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="elementSelector">值选择器</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static async Task<DisposableConcurrentDictionary<TKey, TElement>> ToDisposableConcurrentDictionaryAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector, TElement defaultValue) where TElement : IDisposable
|
|
public static async Task<DisposableConcurrentDictionary<TKey, TElement>> ToDisposableConcurrentDictionaryAsync<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, Task<TElement>> elementSelector, TElement defaultValue) where TElement : IDisposable
|
|
{
|
|
{
|
|
- var dic = new DisposableConcurrentDictionary<TKey, TElement>() { FallbackValue = defaultValue };
|
|
|
|
|
|
+ var dic = new DisposableConcurrentDictionary<TKey, TElement>()
|
|
|
|
+ {
|
|
|
|
+ FallbackValue = defaultValue
|
|
|
|
+ };
|
|
await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
|
|
await source.ForeachAsync(async item => dic[keySelector(item)] = await elementSelector(item));
|
|
return dic;
|
|
return dic;
|
|
}
|
|
}
|
|
@@ -1201,54 +1145,48 @@ namespace Masuit.Tools
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 转换成并发字典集合
|
|
/// 转换成并发字典集合
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
|
- /// <param name="dic"></param>
|
|
|
|
- /// <returns></returns>
|
|
|
|
public static NullableConcurrentDictionary<TKey, TValue> AsConcurrentDictionary<TKey, TValue>(this Dictionary<TKey, TValue> dic) => dic;
|
|
public static NullableConcurrentDictionary<TKey, TValue> AsConcurrentDictionary<TKey, TValue>(this Dictionary<TKey, TValue> dic) => dic;
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 转换成并发字典集合
|
|
/// 转换成并发字典集合
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
|
/// <param name="dic"></param>
|
|
/// <param name="dic"></param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static NullableConcurrentDictionary<TKey, TValue> AsConcurrentDictionary<TKey, TValue>(this Dictionary<TKey, TValue> dic, TValue defaultValue)
|
|
public static NullableConcurrentDictionary<TKey, TValue> AsConcurrentDictionary<TKey, TValue>(this Dictionary<TKey, TValue> dic, TValue defaultValue)
|
|
{
|
|
{
|
|
- var nullableDictionary = new NullableConcurrentDictionary<TKey, TValue>() { FallbackValue = defaultValue };
|
|
|
|
|
|
+ var nullableDictionary = new NullableConcurrentDictionary<TKey, TValue>()
|
|
|
|
+ {
|
|
|
|
+ FallbackValue = defaultValue
|
|
|
|
+ };
|
|
foreach (var p in dic)
|
|
foreach (var p in dic)
|
|
{
|
|
{
|
|
nullableDictionary[p.Key] = p.Value;
|
|
nullableDictionary[p.Key] = p.Value;
|
|
}
|
|
}
|
|
|
|
+
|
|
return nullableDictionary;
|
|
return nullableDictionary;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 转换成普通字典集合
|
|
/// 转换成普通字典集合
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
|
- /// <param name="dic"></param>
|
|
|
|
- /// <returns></returns>
|
|
|
|
public static NullableDictionary<TKey, TValue> AsDictionary<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dic) => dic;
|
|
public static NullableDictionary<TKey, TValue> AsDictionary<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dic) => dic;
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// 转换成普通字典集合
|
|
/// 转换成普通字典集合
|
|
/// </summary>
|
|
/// </summary>
|
|
- /// <typeparam name="TKey"></typeparam>
|
|
|
|
- /// <typeparam name="TValue"></typeparam>
|
|
|
|
/// <param name="dic"></param>
|
|
/// <param name="dic"></param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
/// <param name="defaultValue">键未找到时的默认值</param>
|
|
- /// <returns></returns>
|
|
|
|
public static NullableDictionary<TKey, TValue> AsDictionary<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dic, TValue defaultValue)
|
|
public static NullableDictionary<TKey, TValue> AsDictionary<TKey, TValue>(this ConcurrentDictionary<TKey, TValue> dic, TValue defaultValue)
|
|
{
|
|
{
|
|
- var nullableDictionary = new NullableDictionary<TKey, TValue>() { FallbackValue = defaultValue };
|
|
|
|
|
|
+ var nullableDictionary = new NullableDictionary<TKey, TValue>()
|
|
|
|
+ {
|
|
|
|
+ FallbackValue = defaultValue
|
|
|
|
+ };
|
|
foreach (var p in dic)
|
|
foreach (var p in dic)
|
|
{
|
|
{
|
|
nullableDictionary[p.Key] = p.Value;
|
|
nullableDictionary[p.Key] = p.Value;
|
|
}
|
|
}
|
|
|
|
+
|
|
return nullableDictionary;
|
|
return nullableDictionary;
|
|
}
|
|
}
|
|
}
|
|
}
|