public static class Extension
    {
        #region Base

        public static String Ex_ToString(this DateTime T)
        {
            return T.ToString("yyyy-MM-dd HH:mm:ss");
        }
        public static String Ex_ToString(this DateTime? T)
        {
            return T.Ex_ToString("yyyy-MM-dd HH:mm:ss");
        }
        public static String Ex_ToString(this DateTime? T, String Format)
        {
            return T.Ex_ToString(Format, "");
        }
        public static String Ex_ToString(this DateTime? T, String Format, String Default)
        {
            if (T == null) return Default;
            return T.HasValue ? T.Value.ToString(String.IsNullOrEmpty(Format) ? "yyyy-MM-dd HH:mm:ss" : Format) : Default;
        }

        public static Decimal Ex_ToDecimal(this Object O)
        {
            if (O == null) return 0;
            var R = 0m;
            Decimal.TryParse(O.ToString(), out R);
            return R;
        }
        public static Decimal Ex_ToDecimal(this Decimal? T)
        {
            return T.Ex_ToDecimal(2);
        }
        public static Decimal Ex_ToDecimal(this Decimal? T, Int32 Decimals)
        {
            return T.Ex_ToDecimal(Decimals, 0m);
        }
        public static Decimal Ex_ToDecimal(this Decimal? T, Int32 Decimals, Decimal Default)
        {
            if (T == null) return Default;
            return Math.Round(T.Value, Decimals);
        }


        public static DateTime Ex_ToSqlData(this DateTime Time)
        {
            return Time > System.Data.SqlTypes.SqlDateTime.MinValue.Value ? Time : System.Data.SqlTypes.SqlDateTime.MinValue.Value;
        }

        public static Double Ex_ToLinuxData(this DateTime Time)
        {
            double intResult = 0;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            intResult = (Time - startTime).TotalSeconds;
            return intResult;
        }
        public static Double Ex_ToLinuxData()
        {
            return DateTime.Now.Ex_ToLinuxData();
        }

        public static String Ex_ToJson<T>(this T E)
        {
            String Json = "";
            if (E != null)
                Json = (new JavaScriptSerializer()).Serialize(E);
            return Json;
        }
        public static T Ex_ToEntity<T>(this String Json)
        {
            return (new JavaScriptSerializer()).Deserialize<T>(Json);
        }

        public static String Ex_ToString(this String S, Int32 MaxLength)
        {
            return S.Ex_ToString(MaxLength, "");
        }
        public static String Ex_ToString(this String S, Int32 MaxLength, String ExtensionStr)
        {
            S = S ?? "";
            return S.Length >= MaxLength ? S.Substring(0, MaxLength) + "" : S;
        }
        public static IEnumerable<String> Ex_ToList<T>(this String S)
        {
            return S.Ex_ToList(',', ' ', '\n', '\r', '\t');
        }
        public static IEnumerable<String> Ex_ToList(this String String, params Char[] SplitArg)
        {
            if (String.IsNullOrEmpty(String)) return new List<string>();
            return String.Split(SplitArg, StringSplitOptions.RemoveEmptyEntries);
        }
        public static IEnumerable<String> Ex_ToList(String String, params String[] SplitArg)
        {
            if (String.IsNullOrEmpty(String)) return new List<string>();
            return String.Split(SplitArg, StringSplitOptions.RemoveEmptyEntries);
        }

        public static Int32 Ex_ToInt32(this String S)
        {
            if (S == null) return 0;
            var R = 0;
            Int32.TryParse(S, out R);
            return R;
        }

        public static IEnumerable<Int32> Ex_ToInt32<T>(this IEnumerable<T> L)
        {
            if (L == null) return new List<int>();
            var R = L.Select(p => p.ToString().Ex_ToInt32());
            return R;
        }


        public static IEnumerable<T> Ex_ToList<T>(this IEnumerable<IEnumerable<T>> L)
        {
            var R = new List<T>();
            foreach (var I in L)
            {
                R.AddRange(I);
            }
            return R;
        } 
        #endregion

        #region SelectListItem
        public static String Ex_GetText(this IEnumerable<SelectListItem> iList, Int32? Value)
        {
            var item = iList.FirstOrDefault(p => p.Value == Value.ToString());
            if (item != null) return item.Text;
            return "--";
        }
        public static Int32  Ex_GetValue(this IEnumerable<SelectListItem> iList, String Text)
        {
            var item = iList.FirstOrDefault(p => p.Text == Text);
            if (item != null) return item.Value.Ex_ToInt32();
            return 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="iList"></param>
        /// <param name="Text"></param>
        /// <param name="Value"></param>
        /// <param name="Index">从0开始</param>
        /// <returns></returns>
        public static IEnumerable<SelectListItem>  Ex_Add(this IEnumerable<SelectListItem> iList, String Text, String Value, Int32 Index)
        {
            var List = iList.ToList();
            Index = Index > List.Count ? List.Count : Index;
            List.Insert(Index, new SelectListItem { Text = Text, Value = Value });
            return List;
        }
        public static IEnumerable<SelectListItem>  Ex_AddDefault(this IEnumerable<SelectListItem> iList)
        {
            var List = iList.ToList();
            List.Insert(0, new SelectListItem { Text = "--全部--", Value = "" });
            return List;
        }
        public static IEnumerable<SelectListItem>  Ex_SetSelected(this IEnumerable<SelectListItem> iList, Int32? Value)
        {
            var List = iList.ToList();
            List = List.Select(p =>
            {
                if (p.Value == (Value + "")) p.Selected = true; return p;
            }).ToList();
            return List;
        }
        public static IEnumerable<SelectListItem>  Ex_SetSelected(this IEnumerable<SelectListItem> iList, String Text)
        {
            var List = iList.ToList();
            List = List.Select(p =>
            {
                if (p.Text == Text) p.Selected = true; return p;
            }).ToList();
            return List;
        }
        
        #endregion

        #region Datatable<=>List

        ///// <summary>    

        ///// 转化一个DataTable    

        ///// </summary>    

        ///// <typeparam name="T"></typeparam>    
        ///// <param name="list"></param>    
        ///// <returns></returns>    
        //public static DataTable ToDataTable<T>(this IEnumerable<T> list)
        //{

        //    //创建属性的集合    
        //    List<PropertyInfo> pList = new List<PropertyInfo>();
        //    //获得反射的入口    

        //    Type type = typeof(T);
        //    DataTable dt = new DataTable();
        //    //把所有的public属性加入到集合 并添加DataTable的列    
        //    Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
        //    foreach (var item in list)
        //    {
        //        //创建一个DataRow实例    
        //        DataRow row = dt.NewRow();
        //        //给row 赋值    
        //        pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
        //        //加入到DataTable    
        //        dt.Rows.Add(row);
        //    }
        //    return dt;
        //}


        ///// <summary>    
        ///// DataTable 转换为List 集合    
        ///// </summary>    
        ///// <typeparam name="TResult">类型</typeparam>    
        ///// <param name="dt">DataTable</param>    
        ///// <returns></returns>    
        //public static List<T> ToList<T>(this DataTable dt) where T : class, new()
        //{
        //    //创建一个属性的列表    
        //    List<PropertyInfo> prlist = new List<PropertyInfo>();
        //    //获取TResult的类型实例  反射的入口    

        //    Type t = typeof(T);

        //    //获得TResult 的所有的Public 属性 并找出TResult属性和DataTable的列名称相同的属性(PropertyInfo) 并加入到属性列表     
        //    Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });

        //    //创建返回的集合    

        //    List<T> oblist = new List<T>();

        //    foreach (DataRow row in dt.Rows)
        //    {
        //        //创建TResult的实例    
        //        T ob = new T();
        //        //找到对应的数据  并赋值    
        //        prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); });
        //        //放入到返回的集合中.    
        //        oblist.Add(ob);
        //    }
        //    return oblist;
        //}




        ///// <summary>    
        ///// 将集合类转换成DataTable    
        ///// </summary>    
        ///// <param name="list">集合</param>    
        ///// <returns></returns>    
        //public static DataTable ToDataTableRow(IList list)
        //{
        //    DataTable result = new DataTable();
        //    if (list.Count > 0)
        //    {
        //        PropertyInfo[] propertys = list[0].GetType().GetProperties();

        //        foreach (PropertyInfo pi in propertys)
        //        {
        //            result.Columns.Add(pi.Name, pi.PropertyType);
        //        }
        //        for (int i = 0; i < list.Count; i++)
        //        {
        //            ArrayList tempList = new ArrayList();
        //            foreach (PropertyInfo pi in propertys)
        //            {
        //                object obj = pi.GetValue(list[i], null);
        //                tempList.Add(obj);
        //            }
        //            object[] array = tempList.ToArray();
        //            result.LoadDataRow(array, true);
        //        }
        //    }
        //    return result;
        //}

        ///**/

        ///// <summary>    
        ///// 将泛型集合类转换成DataTable    

        ///// </summary>    
        ///// <typeparam name="T">集合项类型</typeparam>    

        ///// <param name="list">集合</param>    
        ///// <returns>数据集(表)</returns>    
        //public static DataTable ToDataTable<T>(IList<T> list)
        //{
        //    return ToDataTable<T>(list, null);
        //}

        ///**/
        ///// <summary>    
        ///// 将泛型集合类转换成DataTable    
        ///// </summary>    
        ///// <typeparam name="T">集合项类型</typeparam>    
        ///// <param name="list">集合</param>    
        ///// <param name="propertyName">需要返回的列的列名</param>    
        ///// <returns>数据集(表)</returns>    
        //public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
        //{
        //    List<string> propertyNameList = new List<string>();
        //    if (propertyName != null)
        //        propertyNameList.AddRange(propertyName);
        //    DataTable result = new DataTable();
        //    if (list.Count > 0)
        //    {
        //        PropertyInfo[] propertys = list[0].GetType().GetProperties();
        //        foreach (PropertyInfo pi in propertys)
        //        {

        //            if (propertyNameList.Count == 0)
        //            {
        //                //if (pi.PropertyType==typeof(Nullable<>))
        //                //{
        //                result.Columns.Add(pi.Name);
        //                //}
        //                //else
        //                //    result.Columns.Add(pi.Name, pi.PropertyType);
        //            }
        //            else
        //            {
        //                if (propertyNameList.Contains(pi.Name))
        //                    result.Columns.Add(pi.Name, pi.PropertyType);
        //            }
        //        }

        //        for (int i = 0; i < list.Count; i++)
        //        {
        //            ArrayList tempList = new ArrayList();
        //            foreach (PropertyInfo pi in propertys)
        //            {
        //                if (propertyNameList.Count == 0)
        //                {
        //                    object obj = pi.GetValue(list[i], null);
        //                    tempList.Add(obj);
        //                }
        //                else
        //                {
        //                    if (propertyNameList.Contains(pi.Name))
        //                    {
        //                        object obj = pi.GetValue(list[i], null);
        //                        tempList.Add(obj);
        //                    }
        //                }
        //            }
        //            object[] array = tempList.ToArray();
        //            result.LoadDataRow(array, true);
        //        }
        //    }
        //    return result;
        //}
        #endregion

    }