ListEx.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. }
  43. }