.net工具类——字符串(List与字符串的转换)

.net工具类——字符串(List与字符串的转换)

  1. strToList:字符串转为List
  2. ListToStr:List转为字符串
  3. ListToIn:List转为sql的in('','')

 

        /// <summary>
        /// 把idList转换成List
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static List<string> strToList(string idList)
        {
            List<string> list = new List<string>();
            if (!string.IsNullOrEmpty(idList))
            {
                list = idList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p).ToList();
            }
            return list;
        }

        /// <summary>
        /// 把idList转换成List
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static List<int?> strToList_int(string idList)
        {
            List<int?> list = new List<int?>();
            if (!string.IsNullOrEmpty(idList))
            {
                list = idList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.StrToInt()).ToList();
            }
            return list;
        }

        /// <summary>
        /// 把idList转换成List
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static List<Guid> strToList_Guid(string idList)
        {
            List<Guid> list = new List<Guid>();
            if (!string.IsNullOrEmpty(idList))
            {
                list = idList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.StrToGuid() ?? Guid.Empty).ToList();
            }
            return list;
        }

        /// <summary>
        /// 把List转换成idList
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static string ListToStr(List<int?> list)
        {
            if (list != null && list.Count > 0)
            {
                string str = string.Join(",", list.Where(d => d.HasValue));
                return str;
            }
            return null;
        }

        /// <summary>
        /// 把List转换成idList
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static string ListToStr(List<Guid> list)
        {
            if (list != null && list.Count > 0)
            {
                string str = string.Join(",", list);
                return str;
            }
            return null;
        }

        /// <summary>
        /// 把List转换成idList
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static string ListToStr(List<string> list)
        {
            if (list != null && list.Count > 0)
            {
                string str = string.Join(",", list);
                return str;
            }
            return null;
        }

        /// <summary>
        /// 把List转换成idList
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static string ListToStr(HashSet<string> list)
        {
            if (list != null && list.Count > 0)
            {
                string str = string.Join(",", list);
                return str;
            }
            return null;
        }

        /// <summary>
        /// 把List转换成Sql的in('','')
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static string ListToIn(List<string> list)
        {
            if (list != null && list.Count > 0)
            {
                string str = "(";
                foreach (var item in list)
                {
                    str += string.Format("'{0}',", item);
                }
                str = str.Substring(0, str.Length - 1);
                str += ")";
                return str;
            }

            return "";
        }

        /// <summary>
        /// 把List转换成Sql的in('','')
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public static string ListToIn(List<Guid> list)
        {
            if (list != null && list.Count > 0)
            {
                string str = "(";
                foreach (var item in list)
                {
                    str += string.Format("'{0}',", item);
                }
                str = str.Substring(0, str.Length - 1);
                str += ")";
                return str;
            }

            return "";
        }

 

posted @ 2020-08-13 13:33  小确幸123  阅读(1193)  评论(0编辑  收藏  举报