namespace JNPF.Extras.CollectiveOAuth.Enums;
///
/// 数组,队列对象的列表类.
///
public static class ArrayExtensions
{
///
/// 扩展 Dictionary 根据Value反向查找Key的方法.
///
public static T1 Get(this IEnumerable> list, T2 t2)
{
foreach (KeyValuePair obj in list)
if (obj.Value.Equals(t2)) return obj.Key;
return default(T1);
}
///
/// 扩展数组方法 可以在前或者后插入一个对象.
///
/// 对象.
///
/// 要插入的对象.
/// 位置 after/before.
///
public static IEnumerable Inject(this IEnumerable list, T obj, ArrayInjectPlace place = ArrayInjectPlace.Top)
{
T[] list2 = new T[list.Count() + 1];
int index = 0;
foreach (T t in list)
{
list2[place == ArrayInjectPlace.Bottom ? index : index + 1] = t;
}
list2[place == ArrayInjectPlace.Bottom ? list.Count() : 0] = obj;
return list2;
}
///
/// 将数组合并成为一个字符串.
///
public static string Join(this IEnumerable list, char? c = ',')
{
return list.Join(c.ToString());
}
public static string Join(this IEnumerable list, string split)
{
return string.Join(split, list);
}
///
/// 按指定条件过滤数组.
///
/// 默认为过滤重复.
public static IEnumerable Filter(this IEnumerable list, ArrayFilterRule filterRule = ArrayFilterRule.NoRepeater)
{
List list2 = new List();
foreach (T t in list)
{
if (!list2.Contains(t)) list2.Add(t);
}
return list2;
}
///
/// 合并数组 并且去除重复项.
///
/// 要比较的字段.
public static List Merge(this IEnumerable objList, Converter converter, params IEnumerable[] objs)
{
List list = objList.ToList();
foreach (var obj in objs)
{
list.AddRange(obj.ToList().FindAll(t => !list.Exists(t1 => converter(t1).Equals(converter(t)))));
}
return list;
}
///
/// 获取数组的索引项。 如果超出则返回类型的默认值.
///
public static T GetIndex(this IEnumerable list, int index)
{
if (list == null || index >= list.Count() || index < 0) return default(T);
return list.ToArray()[index];
}
///
/// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串.
///
///
///
///
public static string ToQueryString(this IEnumerable> list)
{
return list.ToList().ConvertAll(t => string.Format("{0}={1}", t.Key, t.Value)).Join("&");
}
///
/// 除去数组中的空值和指定名称的参数并以字母a到z的顺序排序.
///
/// 过滤规则 默认做为空判断
public static IEnumerable> Filter(this IEnumerable> list, Func filter = null)
{
if (filter == null)
{
filter = (key, value) =>
{
return !string.IsNullOrEmpty(key.ToString()) && value != null;
};
}
foreach (var item in list)
{
if (filter(item.Key, item.Value))
yield return new KeyValuePair(item.Key, item.Value);
}
}
///
/// 不包含指定的Key.
///
public static IEnumerable> Filter(this IEnumerable> list, params TKey[] filter)
{
return list.Filter((key, value) =>
{
return !string.IsNullOrEmpty(key.ToString()) && value != null && !filter.Contains(key);
});
}
///
/// 按照Key从小大大拍列.
///
///
///
///
public static void Sort(this IEnumerable> list)
{
list = list.ToList().OrderBy(t => t.Key);
}
///
/// 获取父级的继承树(最多支持32级)
/// 包括自己.
///
/// 主键类型.
/// 要查找的数组对象
/// 当前对象的主键值
/// 主键字段
/// 父级字段
///
public static List GetParent(this List obj, TValue id, Func value, Func parent)
{
int count = 0;
T t = obj.Find(m => value.Invoke(m).Equals(id));
List list = new List();
while (t != null)
{
if (count > 32) break;
list.Add(t);
t = obj.Find(m => value.Invoke(m).Equals(parent.Invoke(t)));
count++;
}
return list;
}
///
/// 获取子集列表(包括自己).
///
public static void GetChild(this List obj, TValue id, Func value, Func parent, ref List list)
{
if (list == null) list = new List();
var objT = obj.Find(t => value.Invoke(t).Equals(id));
if (objT != null)
{
list.Add(objT);
foreach (T t in obj.FindAll(m => parent.Invoke(m).Equals(id)))
{
obj.GetChild(value.Invoke(t), value, parent, ref list);
}
}
}
///
/// 获取树形结构的子集执行方法.
///
///
///
/// 当前对象
/// 当前父节点
/// 获取主键的委托
/// 获取父值的委托
/// 委托执行的方法 int 为当前的深度
/// 当前的深度
public static void GetTree(this List obj, TValue id, Func value, Func parent, Action action, int depth = 0)
{
foreach (T t in obj.FindAll(m => parent.Invoke(m).Equals(id)))
{
action.Invoke(t, depth + 1);
obj.GetTree(value.Invoke(t), value, parent, action, depth + 1);
}
}
}
///
/// 数组过滤规则.
///
public enum ArrayFilterRule
{
///
/// 过滤重复.
///
NoRepeater
}
///
/// 插入数组的位置.
///
public enum ArrayInjectPlace
{
///
/// 在顶部插入.
///
Top,
///
/// 在尾部追加.
///
Bottom
}