using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
//namespace Utils.Datas
|
|
namespace System.Collections.Generic
|
|
{
|
|
public static class DictionaryUtil
|
|
{
|
|
/// <summary>
|
|
/// 将第二个参数中的数据合并到第一个参数中。【通过Key判断。如果已存在,则忽略】
|
|
/// <para>Key不能为类</para>
|
|
/// </summary>
|
|
public static void Combine<T1, T2>(this IDictionary<T1, T2> a, IDictionary<T1, T2> b)
|
|
{
|
|
if (a == null)
|
|
a = new Dictionary<T1, T2>();
|
|
if (b == null || b.Count == 0)
|
|
return;
|
|
foreach (var item in b) {
|
|
if (!a.Keys.Contains(item.Key))
|
|
a.Add(item.Key, item.Value);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 找出两个参数中,Key相同的项的Key
|
|
/// <para>Key不能为类</para>
|
|
/// </summary>
|
|
public static List<T1> FindSameKey<T1, T2>(this IDictionary<T1, T2> a, IDictionary<T1, T2> b)
|
|
{
|
|
List<T1> result = new List<T1>();
|
|
foreach (var item in a) {
|
|
if (b.Keys.Contains(item.Key))
|
|
result.Add(item.Key);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从第一个参数中去掉第二个参数中的所有项后,剩余项的Key。只比较Key
|
|
/// <para>Key不能为类</para>
|
|
/// </summary>
|
|
public static List<T1> SubKey<T1, T2>(this IDictionary<T1, T2> a, IDictionary<T1, T2> b)
|
|
{
|
|
List<T1> result = new List<T1>();
|
|
foreach (var item in a) {
|
|
if (!b.Keys.Contains(item.Key))
|
|
result.Add(item.Key);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static Dictionary<T1, string> ChangeValueToString<T1>(this IDictionary<T1, object> dic)
|
|
{//TODO:添加测试
|
|
Dictionary<T1, string> result = new Dictionary<T1, string>();
|
|
foreach (var detail in dic) {
|
|
result.Add(detail.Key, detail.Value == null ? string.Empty : detail.Value.ToString());
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|