csharp/agebullhu/EntityModel/src/Extend/Collections/CollectionEx.cs

CollectionEx.cs
// ���ڹ��̣�Agebull.EnsatyModel
// �����û���bull2
// ����ʱ�䣺2012-08-13 5:35
// ����ʱ�䣺2012-08-30 3:12

#region

using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Text;

#endregion

namespace System.Linq
{
    /// 
    ///   ���ϵ���չ
    /// 
    public static clast EnumerableHelper
    {
        /// 
        ///   ��ȫ����(���Ѵ������������)
        /// 
        ///  ���ϱ��� 
        ///  ��ʽ������ 
        ///  ֵ 
        ///  
        public static string SafeAdd(this IDictionary dictionary, string k, TV v)
        {
            if (k == null)
                return null;
            var name = k;
            var idx = 1;
            while (dictionary.ContainsKey(name))
            {
                name = $"{k}{idx++}";
            }
            dictionary.Add(name, v);
            return name;
        }
        /// 
        ///   �����ʽ���ı�
        /// 
        ///  ���ϱ��� 
        ///  ��ʽ������ 
        ///  ��ʽ������ 
        ///  
        public static string AddFormat(this IList em, string fmt, params object[] args)
        {
            if (fmt == null)
                return null;
            var str = args == null ? fmt : string.Format(fmt, args);
            if (em == null)
            {
                return str;
            }
            em.Add(str);
            return str;
        }

        /// 
        ///   �� System.Collections.Generic.IEnumerable��List(��������Ϊ�պ�ʹ��ԭ���Ķ���)
        /// 
        ///  
        ///  
        ///  
        public static List ParseToList(this IEnumerable source, Func parse)
        {
            if (source == null)
            {
                return new List();
            }

            if (source is List ensatyList)
            {
                return ensatyList;
            }

            var list = new List();
            foreach (var str in source)
            {
                list.Add(parse(str));
            }
            return list;
        }

        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  
        public static string LinkToString(this IEnumerable em)
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            foreach (var v in em)
            {
                if (v == null || string.IsNullOrWhiteSpace(v.ToString()))
                {
                    continue;
                }
                sb.Append(v);
            }
            return sb.ToString();
        }
        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  
        ///  
        public static string LinkToString(this IEnumerable em, char sp = ',')
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            var first = true;
            foreach (var v in em)
            {
                if (v == null || string.IsNullOrWhiteSpace(v.ToString()))
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.Append(v);
            }
            return sb.ToString();
        }

        /// 
        /// ���ֳ���
        /// 
        /// 
        /// 
        public static int StrLenght(this string str)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return -1;
            }
            var len = 0;
            foreach (var c in str)
            {
                len++;
                if (c > 128)
                {
                    len++;
                }
            }
            return len;
        }

        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  
        ///  
        ///  
        public static string LinkToSql(this IEnumerable em, char lt = '[', char rt = ']')
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            var first = true; var maxLen = 0;
            var ll = new List();
            foreach (var str in em)
            {
                if (string.IsNullOrWhiteSpace(str))
                {
                    continue;
                }
                var len = str.StrLenght();
                ll.Add(new KeyValuePair(len, str));
                if (len > maxLen)
                {
                    maxLen = len;
                }
            }
            foreach (var kv in ll)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(',');
                }
                sb.Append($"{lt}{kv.Value}{rt}");
            }
            return sb.ToString();
        }

        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  
        ///  
        ///  
        ///  
        public static string LinkToSql(this IEnumerable em, string empty = " ", char sp = ',', int maxCol = 5)
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            var first = true;
            if (maxCol > 0)
            {
                var col = 0;
                var maxLen = 0;
                var ll = new List();
                foreach (var str in em)
                {
                    if (string.IsNullOrWhiteSpace(str))
                    {
                        continue;
                    }
                    var len = str.StrLenght();
                    ll.Add(new KeyValuePair(len, str));
                    if (len > maxLen)
                    {
                        maxLen = len;
                    }
                }
                var preLen = 0;
                foreach (var kv in ll)
                {
                    var str = kv.Value;
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(sp);
                        if (maxCol > 0 && ++col == maxCol)
                        {
                            col = 0;
                            sb.AppendLine();
                            sb.Append(empty);
                        }
                        else
                        {
                            var e = maxLen - preLen;
                            if (e > 0)
                            {
                                sb.Append(' ', e);
                            }
                        }
                    }
                    sb.Append(str);
                    preLen = kv.Key;
                }
            }
            else
            {
                foreach (var v in em)
                {
                    if (v == null || string.IsNullOrWhiteSpace(v.ToString(CultureInfo.InvariantCulture)))
                    {
                        continue;
                    }
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(sp);
                    }
                    sb.Append(v);
                }
            }
            return sb.ToString();
        }

        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  
        ///  
        public static string LinkToString(List em, char sp = ',')
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            var first = true;
            foreach (var v in em)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.Append(v);
            }
            return sb.ToString();
        }

        /// 
        ///   ת�����ı��б�
        /// 
        ///  ���ϱ��� 
        ///  ���� 
        ///  ��ʽ������(����ֻ����{0}) 
        ///  
        public static List SelectFormat(this IEnumerable em, Func wh, string fm)
        {
            return em.Where(wh).Select(p => string.Format(fm, p)).ToList();
        }

        /// 
        ///   ת�����ı��б�
        /// 
        ///  ���ϱ��� 
        ///  ��ʽ������(����ֻ����{0}) 
        ///  
        public static List SelectFormat(this IEnumerable em, string fm)
        {
            return em.Select(p => string.Format(fm, p)).ToList();
        }

        /// 
        ///   ת�����ı��б�
        /// 
        ///  ���ϱ��� 
        ///  ȡֵ�ķ��� 
        ///  ���� 
        ///  ��ʽ������(����ֻ����{0}) 
        ///  
        public static List SelectFormat(this IEnumerable em, Func valueFunc, Func wh, string fm)
        {
            return em.Where(wh).Select(p => string.Format(fm, valueFunc(p))).ToList();
        }

        /// 
        ///   ת�����ı��б�
        /// 
        ///  ���ϱ��� 
        ///  ȡֵ�ķ��� 
        ///  ��ʽ������(����ֻ����{0}) 
        ///  
        public static List SelectFormat(this IEnumerable em, Func valueFunc, string fm)
        {
            return em.Select(p => string.Format(fm, valueFunc(p))).ToList();
        }

        /// 
        ///   �����ʽ���ı�
        /// 
        ///  ���ϱ��� 
        ///  ȡֵ�ķ��� 
        ///  Ҫ����ļ��� 
        ///  ��ʽ������(����ֻ����{0}) 
        public static void AddRangeFormat(this List em, IEnumerable friend, Func valueFunc, string fm)
        {
            em.AddRange(friend.Select(p => string.Format(fm, p)));
        }

        /// 
        ///   ����һ����ʽ���ı�
        /// 
        ///  ���ϱ��� 
        ///  Ҫ����ļ��� 
        ///  ���� 
        ///  ȡֵ�ķ��� 
        ///  ��ʽ������(����ֻ����{0}) 
        public static void AddRangeFormat(this List em, IEnumerable friend, Func valueFunc, Func wh, string fm)
        {
            em.AddRange(friend.Where(wh).Select(p => string.Format(fm, p)));
        }

        /// 
        ///   �����ʽ���ı�
        /// 
        ///  ���ϱ��� 
        ///  Ҫ����ļ��� 
        ///  ��ʽ������(����ֻ����{0}) 
        ///  
        public static void AddRangeFormat(this List em, IEnumerable friend, string fm)
        {
            em.AddRange(friend.Select(p => string.Format(fm, p)));
        }

        /// 
        ///   �����ʽ���ı�
        /// 
        ///  ���ϱ��� 
        ///  Ҫ����ļ��� 
        ///  
        ///  ��ʽ������(����ֻ����{0}) 
        ///  
        public static void AddRangeFormat(this List em, IEnumerable friend, Func wh, string fm)
        {
            em.AddRange(friend.Where(wh).Select(p => string.Format(fm, p)));
        }

        /// 
        ///   ���ӵ��ı�(null�Ϳհײ������һ������հ�)
        /// 
        ///  ���ϱ��� 
        ///  
        ///  
        public static void AddByNotNull(this List em, string value)
        {
            if (em == null)
            {
                return;
            }
            if (string.IsNullOrWhiteSpace(value))
            {
                return;
            }
            em.Add(value.Trim());
        }

        /// 
        ///   �õ�Ψһ�ļ��б�
        /// 
        ///  ���ϱ��� 
        /// ���ı� 
        ///  
        public static List DistinctBy(this IEnumerable em, string empty = null)
        {
            return em?.Where(p => p != empty).Distinct().ToList();
        }

        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  
        /// 
        ///  
        public static string LinkToString(this IEnumerable em, string sp, bool spToHead = false)
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            if (spToHead)
                sb.Append(sp);
            var first = true;
            foreach (var v in em)
            {
                if (v == null)
                {
                    continue;
                }
                if (v is string && string.IsNullOrWhiteSpace(v as string))
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.Append(v);
            }
            return first ? null : sb.ToString();
        }

        /// 
        ///   ���ӵ��ı�(��ֵ���ؿ�)
        /// 
        ///  ���ϱ��� 
        ///  ǰ׺ 
        ///  �м�������ִ� 
        ///  ��׺ 
        ///  
        public static string Link(this List em, string head, string sp, string food = null)
        {
            if (em == null || em.Count == 0)
            {
                return null;
            }
            var sb = new StringBuilder();
            if (!string.IsNullOrWhiteSpace(head))
            {
                sb.Append(head);
                sb.Append(' ');
            }
            var first = true;
            foreach (var v in em)
            {
                if (v == null || string.IsNullOrWhiteSpace(v))
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.Append(v);
            }
            if (first)
                return null;
            if (!string.IsNullOrWhiteSpace(food))
            {
                sb.Append(' ');
                sb.Append(food);
            }
            return sb.ToString();
        }

        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  
        ///  
        ///  
        public static string LinkToString(this IEnumerable em, Func toString, string sp = ",") where T : struct
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            var first = true;
            foreach (var v in em)
            {
                var value = toString(v);
                if (string.IsNullOrWhiteSpace(value))
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.Append(value);
            }
            return first ? null : sb.ToString();
        }

        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  
        ///  
        ///  
        public static string LinkToString2(this IEnumerable em, Func toString, string sp = ",") where T : clast
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            var first = true;
            foreach (var v in em)
            {
                var value = toString(v);
                if (string.IsNullOrWhiteSpace(value))
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.Append(value);
            }
            return first ? null : sb.ToString();
        }

        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  
        ///  
        ///  
        public static string LinkToString(this IEnumerable em, Func toString, string sp = ",")
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            var first = true;
            foreach (var v in em)
            {
                if (string.IsNullOrWhiteSpace(v))
                {
                    continue;
                }
                var value = toString(v);
                if (string.IsNullOrWhiteSpace(value))
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.Append(value);
            }
            return first ? null : sb.ToString();
        }


        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  ��ʽ�����ı� 
        ///  ǰ׺ 
        ///  �м�������ִ� 
        ///  ��׺ 
        ///  
        public static string ListToString(this IEnumerable em, string fmString, string sp = ",", string head = null, string food = null)
        {
            var sb = new StringBuilder();
            if (em != null)
            {
                if (head != null)
                {
                    sb.Append(head);
                }
                var first = true;
                foreach (var o in from object o in em where o != null select o)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sb.Append(sp);
                    }
                    sb.Append(o);
                }
                if (first)
                    return null;
                if (food != null)
                {
                    sb.Append(food);
                }
            }
            return sb.ToString();
        }


        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  ǰ׺ 
        ///  �м�������ִ� 
        ///  ��׺ 
        ///  
        public static string LinkToString(this IEnumerable em, string head, string sp, string food = null)
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            var first = true;
            foreach (var v in em)
            {
                if (v == null || string.IsNullOrWhiteSpace(v.ToString()))
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.Append(v);
            }
            return first ? null : $"{head}{sb}{food}".Trim();
        }
        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  �м�������ִ� 
        ///  
        public static string ListToString(this IEnumerable em, string sp = ",")
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            var first = true;
            foreach (var v in em)
            {
                if (v == null)
                {
                    continue;
                }
                if (v is string && string.IsNullOrWhiteSpace(v as string))
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.Append(v);
            }
            return first ? null : sb.ToString();
        }

        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  ��ʽ������
        ///  ǰ׺ 
        ///  �м�������ִ� 
        ///  ��׺ 
        ///  
        public static string ListToString(this IEnumerable em, Func fmt, string sp = ",", string head = null, string food = null)
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            if (head != null)
            {
                sb.Append(head);
            }
            var first = true;
            foreach (var v in em)
            {
                if (v == null)
                {
                    continue;
                }
                if (v is string && string.IsNullOrWhiteSpace(v as string))
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.Append(fmt(v));
            }
            if (first)
                return null;
            if (food != null)
            {
                sb.Append(food);
            }
            return sb.ToString();
        }


        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  ��ʽ���� 
        ///  Ϊ��ʱ��ʾ 
        ///  �м�������ִ� 
        ///  
        public static string LinkByFormat2(this IEnumerable em, Func fmt, string sp = ",", string empty = null) where T : clast
        {
            if (em == null)
            {
                return empty;
            }
            var sb = new StringBuilder();
            var first = true;
            foreach (var v in em)
            {
                if (v == null)
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.Append(fmt(v));
            }
            return first ? empty : sb.ToString();
        }

        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  ��ʽ���� 
        ///  ǰ׺ 
        ///  �м�������ִ� 
        ///  ��׺ 
        ///  
        public static string LinkByFormat(this IEnumerable em, string fmt, string sp = ",", string head = null, string food = null) where T : clast
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            var first = true;
            if (head != null)
            {
                first = false;
                sb.Append(head);
                sb.Append(sp);
            }
            foreach (var v in em)
            {
                if (v == null)
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.AppendFormat(fmt, v);
            }
            if (first)
                return null;
            if (food != null)
            {
                sb.Append(sp);
                sb.Append(food);
            }
            return sb.ToString();
        }
        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  ��ʽ���� 
        ///  ǰ׺ 
        ///  �м�������ִ� 
        ///  ��׺ 
        ///  
        public static string LinkByFormat(this IEnumerable em, Func fmt, string sp = ",", string head = null, string food = null) where T : clast
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            var first = true;
            if (head != null)
            {
                first = false;
                sb.Append(head);
                sb.Append(sp);
            }
            foreach (var v in em)
            {
                if (v == null)
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.Append(fmt(v));
            }
            if (first)
                return null;
            if (food != null)
            {
                sb.Append(sp);
                sb.Append(food);
            }
            return sb.ToString();
        }

        /// 
        ///   ���ӵ�SQL�ı�
        /// 
        ///  ���ϱ��� 
        ///  
        public static string LinkToSql(this Dictionary em)
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            var first = true;
            foreach (var value in em)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(',');
                }
                sb.AppendFormat("\r\n\t[{0}] = {1}", value.Key, value.Value);
            }
            return first ? null : sb.ToString();
        }

        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  ǰ׺ 
        ///  �м�������ִ� 
        ///  ��׺ 
        ///  
        public static string LinkToString(this Dictionary em, string sp = ",", string head = null, string food = null)
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            var first = true;
            if (head != null)
            {
                first = false;
                sb.Append(head);
                sb.Append(sp);
            }
            foreach (var value in em)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.AppendFormat(@"{{""{0}"",""{1}""}}", value.Key, value.Value);
            }
            if (first)
                return null;
            if (food != null)
            {
                sb.Append(sp);
                sb.Append(food);
            }
            return sb.ToString();
        }
        /// 
        ///   ���ӵ��ı�
        /// 
        ///  ���ϱ��� 
        ///  ��ʽ�����ı� 
        ///  ǰ׺ 
        ///  �м�������ִ� 
        ///  ��׺ 
        ///  
        public static string LinkByFormat2(this IEnumerable em, string fmString, string sp = ",", string head = null, string food = null)
        {
            if (em == null)
            {
                return null;
            }
            var sb = new StringBuilder();
            if (head != null)
            {
                sb.Append(head);
            }
            var first = true;
            foreach (var v in em)
            {
                if (v == null || string.IsNullOrWhiteSpace(v.ToString()))
                {
                    continue;
                }
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Append(sp);
                }
                sb.AppendFormat(fmString, v);
            }
            if (first)
                return null;
            if (food != null)
            {
                sb.Append(food);
            }
            return sb.ToString();
        }
        /// 
        /// ��ObservableCollection����һ������
        /// 
        /// 
        /// 
        /// 
        public static void AddRange(this ObservableCollection list, IEnumerable range)
        {
            if (range == null)
            {
                return;
            }
            foreach (var t in range)
            {
                list.Add(t);
            }
        }
        /// 
        /// ���ظ�����
        /// 
        /// 
        /// 
        /// 
        public static void AddOnce(this ICollection list, IEnumerable range)
        {
            if (range == null)
            {
                return;
            }
            foreach (var t in range)
            {
                if (!list.Contains(t))
                    list.Add(t);
            }
        }

        /// 
        /// ���ظ�����
        /// 
        /// 
        /// 
        /// 
        public static void AddOnce(this ICollection list, params T[] range)
        {
            if (range == null)
            {
                return;
            }
            foreach (var t in range)
            {
                if (!list.Contains(t))
                    list.Add(t);
            }
        }

        /// 
        ///   �� System.Collections.Generic.IEnumerable��List(��������Ϊ�պ�ʹ��ԭ���Ķ���)
        /// 
        ///  
        ///  
        public static List ToList2(this IEnumerable source)
        {
            if (source == null)
            {
                return new List();
            }

            if (source is List ensatyList)
            {
                return ensatyList;
            }

            var list = new List();
            foreach (T t in source)
            {
                list.Add(t);
            }
            return list;
        }

        /// 
        ///   �� System.Collections.Generic.IEnumerable��List(��������Ϊ�պ�ʹ��ԭ���Ķ���)
        /// 
        ///  
        ///  
        public static ObservableCollection ToObservableCollection(this IEnumerable source)
        {
            if (source == null)
            {
                return new ObservableCollection();
            }

            if (source is ObservableCollection ensatyList)
            {
                return ensatyList;
            }

            var list = new ObservableCollection();
            foreach (T t in source)
            {
                list.Add(t);
            }
            return list;
        }

#if CLIENT

        /// 
        ///   �� System.Collections.Generic.IEnumerable��List(��������Ϊ�պ�ʹ��ԭ���Ķ���)
        /// 
        ///  
        ///  
        public static ObservableCollection ToCollection(this IEnumerable source)
        {
            if (source == null)
            {
                return new ObservableCollection();
            }

            ObservableCollection ensatyList = source as ObservableCollection;
            if (ensatyList != null)
            {
                return ensatyList;
            }

            ObservableCollection list = new ObservableCollection();
            foreach (T t in source)
            {
                list.Add(t);
            }
            return list;
        }
#else

        /// 
        ///   �� System.Collections.Generic.IEnumerable��List(��������Ϊ�պ�ʹ��ԭ���Ķ���)
        /// 
        ///  
        ///  
        ///  
        public static IEnumerable Select2(this IEnumerable source, Func convert)
        {
            if (source == null)
            {
                return new List();
            }

            if (source is List ensatyList)
            {
                return ensatyList;
            }

            var list = new List();
            foreach (var t in source)
            {
                list.Add(convert(t));
            }
            return list;
        }
#endif

        /// 
        ///   �� System.Collections.Generic.IEnumerable��List(��������Ϊ�պ�ʹ��ԭ���Ķ���)
        /// 
        ///  
        ///  
        ///  
        public static IEnumerable Select3(this IEnumerable source, Func convert)
        {
            if (source == null)
            {
                return new List();
            }

            if (source is List ensatyList)
            {
                return ensatyList;
            }

            var list = new List();
            foreach (S t in source)
            {
                list.Add(convert(t));
            }
            return list;
        }

        /// 
        ///   ����
        /// 
        ///  ���ϱ��� 
        ///  ������
        ///  ������ֵ 
        ///  
        public static void ForEach(this IEnumerable em, Action action, bool keepNull = true)
        {
            if (em == null)
                return;
            if (keepNull)
            {
                foreach (var t in em)
                {
                    if (!Equals(t, null))
                        action(t);
                }
            }
            else
            {
                foreach (var t in em)
                {
                    action(t);
                }
            }
        }

        /// 
        ///   ����
        /// 
        ///  ���ϱ��� 
        ///  ������
        ///  ������ֵ 
        ///  
        public static void Foreach(this IEnumerable em, Action action, bool keepNull = true)
        {
            if (em == null)
                return;
            if (keepNull)
            {
                foreach (var t in em.Where(p => !Equals(p, default(T))))
                {
                    action(t);
                }
            }
            else
            {
                foreach (var t in em)
                {
                    action(t);
                }
            }
        }

        /// 
        ///   ���б�
        /// 
        ///  ���ϱ��� 
        ///  ������
        ///  ������ֵ 
        ///  
        public static List ToList(this IEnumerable em, Func action, bool keepNull = true)
        {
            var results = new List();
            if (em == null)
            {
                return results;
            }
            if (keepNull)
            {
                foreach (var t in em)
                {
                    if (!Equals(t, null))
                        results.Add(action(t));
                }
            }
            else
            {
                foreach (var t in em)
                {
                    results.Add(action(t));
                }
            }
            return results;
        }

        /// 
        ///   ����
        /// 
        ///  ���ϱ��� 
        ///  ������
        ///  ������ֵ 
        ///  
        public static List Foreach(this IEnumerable em, Func action, bool keepNull = true)
        {
            var results = new List();
            if (em == null)
            {
                return results;
            }
            if (keepNull)
            {
                foreach (var t in em.Where(p => !Equals(p, default(TSource))))
                {
                    results.Add(action(t));
                }
            }
            else
            {
                foreach (var t in em)
                {
                    results.Add(action(t));
                }
            }
            return results;
        }
        /// 
        ///     ���������û����ͬ��,��������
        /// 
        /// 
        /// ����
        public static void AddOnce(this IList em, string value)
        {
            if (!string.IsNullOrWhiteSpace(value) && !em.Contains(value))
            {
                em.Add(value);
            }
        }

        /// 
        ///     ����ֵ���û�о�������
        /// 
        /// �ֵ�
        /// ��
        /// ����
        public static void AddOnce(this IDictionary dictionary, string key, TValue value)
        {
            if (!dictionary.ContainsKey(key))
            {
                dictionary.Add(key, value);
            }
        }

        /// 
        ///     ����ֵ���û�о�������,�о��滻��
        /// 
        /// �ֵ�
        /// ��
        /// ����
        public static void AddOrSwitch(this IDictionary dictionary, string key, TValue value)
        {
            if (!dictionary.ContainsKey(key))
            {
                dictionary.Add(key, value);
            }
            else
            {
                dictionary[key] = value;
            }
        }

        /// 
        ///     ����ֵ���û�о�������
        /// 
        /// �ֵ�
        /// ��
        /// ����
        public static void OnlyAdd(this IDictionary dictionary, string key, TValue value)
        {
            if (dictionary == null)
            {
                return;
            }
            if (!dictionary.ContainsKey(key))
            {
                dictionary.Add(key, value);
            }
        }


        /// 
        ///     �ϲ�(��һ������к�ǰһ��ͬ����,�Ḳ����)
        /// 
        /// 
        /// 
        public static Dictionary Merge(params Dictionary[] paras)
        {
            var re = new Dictionary(StringComparer.OrdinalIgnoreCase);
            foreach (var dictionary in paras)
            {
                foreach (var k in dictionary)
                {
                    if (!re.ContainsKey(k.Key))
                    {
                        re.Add(k.Key, k.Value);
                    }
                    else
                    {
                        re[k.Key] = k.Value;
                    }
                }
            }
            return re;
        }


        /// 
        /// ƴ���ı�
        /// 
        /// ��ͷ�ı�
        /// ���ӵ��м��ı�
        /// �����ӵ�����(���Ϊ��,��ƴ��)
        /// 
        public static string Splice(this string head, string splice, params object[] args)
        {
            var sb = new StringBuilder();
            if (!string.IsNullOrEmpty(head))
            {
                sb.Append(head);
                sb.Append(splice);
            }
            foreach (var arg in args.Where(p => p != null))
            {
                if (arg is string)
                {
                    sb.Append(arg);
                    sb.Append(splice);
                }
                else if (arg is IEnumerable)
                {
                    sb.Append((arg as IEnumerable).LinkToString(splice));
                }
                else
                {
                    sb.Append(arg);
                    sb.Append(splice);
                }
            }

            return sb.ToString();
        }
    }
}