1. 程式人生 > 實用技巧 >.net工具類——字串(List與字串的轉換)

.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 ""; }