zoukankan      html  css  js  c++  java
  • 通用功能类:ConverHelper 数据转换类(数据转换助手)

            /// <summary>
            /// Double转换
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static double ToDouble(this object obj)
            {
                if (obj == null || obj == DBNull.Value || string.IsNullOrEmpty(obj.ToString()))
                    return 0;
                else
                {
                    double value;
                    if (double.TryParse(obj.ToString(), out value))
                        return value;
                    else
                        return 0;
                }
            }
    
            /// <summary>
            /// Int32转换
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static int ToInt(this object obj)
            {
                try
                {
                    if (obj == null || obj == DBNull.Value || string.IsNullOrEmpty(obj.ToString()))
                        return -1;
                    else
                    {
                        if (obj.ToString().ToUpper() == "TRUE")
                            return 1;
                        else if (obj.ToString().ToUpper() == "FALSE")
                            return 0;
                        int data = -1;
                        if (int.TryParse(obj.ToString(), out data))
                        {
                            return data;
                        }
                        else
                            return -1;
                    }
                }
                catch
                {
                    throw;
                }
            }
    
            /// <summary>
            /// Bool转换
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static bool ToBool(this object obj)
            {
                if (obj == null || obj == DBNull.Value || string.IsNullOrEmpty(obj.ToString()))
                    return false;
                else
                {
                    bool value;
                    if (obj.ToStringEx() == "1")
                        return true;
                    else if (obj.ToStringEx() == "0")
                        return false;
                    else if (bool.TryParse(obj.ToString(), out value))
                        return value;
                    else
                        return false;
                }
            }
    
            /// <summary>
            /// Long转换
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static long ToLong(this object obj)
            {
                try
                {
                    if (obj == null || obj == DBNull.Value || string.IsNullOrEmpty(obj.ToString()))
                        return -1;
                    else
                    {
                        long value;
                        if (long.TryParse(obj.ToStringEx(), out value))
                        {
                            return value;
                        }
                        else
                        {
                            return -1;
                        }
                    }
                }
                catch
                {
                    throw;
                }
            }
    
            /// <summary>
            /// String转换
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static string ToStringEx(this object obj)
            {
                try
                {
                    if (obj == null || obj == DBNull.Value)
                        return string.Empty;
                    else
                        return obj.ToString().Trim();
                }
                catch
                {
                    throw;
                }
            }
    
            /// <summary>
            /// DateTime转换
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static DateTime ToDateTime(this object obj)
            {
                if (obj == null || obj == DBNull.Value || obj.ToString() == string.Empty)
                    return new DateTime();
                else
                    return Convert.ToDateTime(obj);
            }
    
            /// <summary>
            /// List int泛型转换为','隔开的字符串
            /// </summary>
            /// <param name="lstObj"></param>
            /// <returns></returns>
            public static string ToListString(this List<int> lstObj)
            {
                if (lstObj == null)
                    return string.Empty;
    
                string strValue = string.Empty;
                foreach (int value in lstObj)
                {
                    if (string.IsNullOrEmpty(strValue))
                        strValue += value.ToString();
                    else
                        strValue += string.Format(",{0}", value);
                }
                return strValue;
            }
    
            /// <summary>
            /// 字符串转换为List int泛型
            /// </summary>
            /// <param name="strObj"></param>
            /// <returns></returns>
            public static List<int> ToIntList(this string strObj)
            {
                List<int> lstValue = new List<int>();
                string[] arrString = strObj.Split(',');
                if (arrString == null || arrString.Length == 0)
                    return lstValue;
    
                foreach (string value in arrString)
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        int intBcch;
                        if (int.TryParse(value, out intBcch))
                        {
                            lstValue.Add(Convert.ToInt32(intBcch));
                        }
                    }
                }
                return lstValue;
            }
    
            /// <summary>
            /// Int List集合转换为字符串:1-3,6,9
            /// </summary>
            /// <param name="lstObj"></param>
            /// <returns></returns>
            public static string ToRangeString(this IList<int> lstFreq)
            {
                if (lstFreq == null || lstFreq.Count <= 0)
                    return "";
    
                lstFreq = lstFreq.OrderBy(item => item).ToList();
                int begin = lstFreq[0];
                int end = lstFreq[0];
                string txtFreq = string.Empty;
    
                for (int i = 1; i <= lstFreq.Count; i++)
                {
                    if (i == lstFreq.Count)
                    {
                        if (lstFreq[i - 1] != begin)
                        {
                            txtFreq += string.Format("{0}-{1}", begin, lstFreq[i - 1]);
                        }
                        else
                        {
                            txtFreq += string.Format("{0}", lstFreq[i - 1]);
                        }
                    }
                    else if (lstFreq[i] != (lstFreq[i - 1] + 1))
                    {
                        end = lstFreq[i - 1];
    
                        if (begin != end)
                        {
                            txtFreq += string.Format("{0}-{1},", begin, end);
                        }
                        else
                        {
                            txtFreq += string.Format("{0},", end);
                        }
                        begin = lstFreq[i];
                        end = lstFreq[i];
                    }
                }
                return txtFreq;
            }
    
            /// <summary>
            /// 字符串转换为频点集合
            /// </summary>
            /// <param name="strPlan"></param>
            /// <returns></returns>
            public static List<int> RangeToList(this string strPlan)
            {
                string[] arrPlan = strPlan.Split(',');
                List<int> lstPlan = new List<int>();
                for (int i = 0; i < arrPlan.Length; i++)
                {
                    if (arrPlan[i].Contains('-'))
                    {
                        string[] data = arrPlan[i].Split('-');
                        if (data.Length == 2)
                        {
                            int first = data[0].ToInt();
                            int last = data[1].ToInt();
                            if (first <= last && first != -1)
                            {
                                for (int x = first; x <= last; x++)
                                {
                                    if (!lstPlan.Contains(x))
                                    {
                                        lstPlan.Add(x);
                                    }
                                }
                            }
                        }
                    }
                    else if (!string.IsNullOrEmpty(arrPlan[i]))
                    {
    
                        int data = arrPlan[i].ToInt();
                        if (!lstPlan.Contains(data) && data != -1)
                        {
                            lstPlan.Add(data);
                        }
                    }
    
                }
                return lstPlan;
            }
    
            /// <summary>
            /// 逗号分割字符串
            /// </summary>
            /// <param name="strObj"></param>
            /// <returns></returns>
            public static List<string> SplitByComma(this string strObj)
            {
                if (string.IsNullOrEmpty(strObj))
                    return new List<string>();
                string[] str = strObj.Trim().Split(',');
                return str.ToList();
            }
    
            /// <summary>
            /// 检测obj,如果为DBNUll或空字符串 返回true
            /// </summary>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static bool CheckIsDBNullOREmpty(this object obj)
            {
                if (obj == null)
                {
                    return true;
                }
                else if (obj.ToString().Trim() == string.Empty)
                {
                    return true;
                }
                return false;
            }
    
            #region IList 与 DataTable 互转
            public static DataTable ConvertTo<T>(this IList<T> list)
            {
                DataTable table = CreateTable<T>();
                Type entityType = typeof(T);
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);
    
                foreach (T item in list)
                {
                    DataRow row = table.NewRow();
    
                    foreach (PropertyDescriptor prop in properties)
                    {
                        row[prop.Name] = prop.GetValue(item);
                    }
    
                    table.Rows.Add(row);
                }
                return table;
            }
    
            public static DataTable CreateTable<T>()
            {
                Type entityType = typeof(T);
                DataTable table = new DataTable(entityType.Name);
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);
    
                foreach (PropertyDescriptor prop in properties)
                {
                    table.Columns.Add(prop.Name, prop.PropertyType);
                }
    
                return table;
            }
    
            public static IList<T> ConvertTo<T>(IList<DataRow> rows)
            {
                IList<T> list = null;
    
                if (rows != null)
                {
                    list = new List<T>();
    
                    foreach (DataRow row in rows)
                    {
                        T item = CreateItem<T>(row);
                        list.Add(item);
                    }
                }
    
                return list;
            }
    
            public static IList<T> ConvertTo<T>(this DataTable table)
            {
                if (table == null)
                {
                    return null;
                }
    
                List<DataRow> rows = new List<DataRow>();
    
                foreach (DataRow row in table.Rows)
                {
                    rows.Add(row);
                }
    
                return ConvertTo<T>(rows);
            }
    
            public static T CreateItem<T>(this DataRow row)
            {
                T obj = default(T);
                if (row != null)
                {
                    obj = Activator.CreateInstance<T>();//string 类型不支持无参的反射
    
                    foreach (DataColumn column in row.Table.Columns)
                    {
                        PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
                        try
                        {
                            object value = row[column.ColumnName];
                            prop.SetValue(obj, value, null);
                        }
                        catch
                        {
                            throw;
                        }
                    }
                }
    
                return obj;
            }
    
            /// <summary>
            /// 该方法仅能转换DataTable的首列
            /// </summary>
            /// <typeparam name="?"></typeparam>
            /// <param name="table"></param>
            /// <returns></returns>
            public static List<string> ConvertToListWithFirstColumn(this DataTable table)
            {
                if (table == null)
                {
                    return null;
                }
                List<string> list = new List<string>();
                foreach (DataRow row in table.Rows)
                {
                    list.Add(row[0].ToString());
                }
                return list;
            }
  • 相关阅读:
    Luogu P1090 合并果子(优先队列 || priority_queue)
    Luogu P1012 拼数
    hibernate5.2的基本配置
    [bzoj1210][HNOI2004]邮递员【插头dp】
    [bzoj3470]Freda’s Walk【概率与期望dp】
    [bzoj4851][Jsoi2016]位运算【矩阵乘法】【状压dp】
    [bzoj4852][Jsoi2016]炸弹攻击【随机化】
    [bzoj4853][Jsoi2016]飞机调度【最短路】【网络流】
    [bzoj4850][Jsoi2016]灯塔【暴力】
    [bzoj4919][Lydsy1706月赛]大根堆【dp】【启发式合并】【stl】
  • 原文地址:https://www.cnblogs.com/lqsilly/p/2877692.html
Copyright © 2011-2022 走看看