.net工具类——字符串(List与字符串的转换)
- strToList:字符串转为List
- ListToStr:List转为字符串
- 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 ""; }