ListEx.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text;
  8. namespace DirectService.Tools
  9. {
  10. internal static class ListEx
  11. {
  12. /// <summary>
  13. /// 把列表转换为(*,*,*,*,...)字符串,空列表返回null
  14. /// </summary>
  15. public static string getString<T>(List<T> list)
  16. {
  17. string listString;
  18. if (list==null||list.Count<=0)
  19. {
  20. return null;
  21. }
  22. StringBuilder sb = new StringBuilder("(");
  23. switch (typeof(T).Name)
  24. {
  25. case "DateTime":
  26. case "String":
  27. foreach (var item in list)
  28. {
  29. sb.Append("'" + item + "',");
  30. }
  31. break;
  32. default:
  33. foreach (var item in list)
  34. {
  35. sb.Append(item + ",");
  36. }
  37. break;
  38. }
  39. listString = sb.ToString().TrimEnd(',')+')';
  40. return listString;
  41. }
  42. public static string GetWhereStr(List<string> whereList, string connector = "AND")
  43. {
  44. string connectStr = " " + connector + " ";
  45. var whereStr = string.Empty;
  46. for (int i = 0; i < whereList.Count; i++)
  47. {
  48. if (i == 0)
  49. {
  50. whereStr += "(" + whereList[i] + ")";
  51. }
  52. else
  53. {
  54. whereStr += connectStr + "(" + whereList[i] + ")";
  55. }
  56. }
  57. return whereStr;
  58. }
  59. }
  60. }