UtilExtensions.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace SqlSugar.Extensions
  6. {
  7. /// <summary>
  8. ///Common Extensions for external users
  9. /// </summary>
  10. public static class UtilExtensions
  11. {
  12. public static int ObjToInt(this object thisValue)
  13. {
  14. int reval = 0;
  15. if (thisValue == null) return 0;
  16. if (thisValue is Enum)
  17. {
  18. return (int)thisValue;
  19. }
  20. if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
  21. {
  22. return reval;
  23. }
  24. return reval;
  25. }
  26. public static int ObjToInt(this object thisValue, int errorValue)
  27. {
  28. int reval = 0;
  29. if (thisValue is Enum)
  30. {
  31. return (int)thisValue;
  32. }
  33. if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
  34. {
  35. return reval;
  36. }
  37. return errorValue;
  38. }
  39. public static double ObjToMoney(this object thisValue)
  40. {
  41. double reval = 0;
  42. if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
  43. {
  44. return reval;
  45. }
  46. return 0;
  47. }
  48. public static double ObjToMoney(this object thisValue, double errorValue)
  49. {
  50. double reval = 0;
  51. if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
  52. {
  53. return reval;
  54. }
  55. return errorValue;
  56. }
  57. public static string ObjToString(this object thisValue)
  58. {
  59. if (thisValue != null) return thisValue.ToString().Trim();
  60. return "";
  61. }
  62. public static string ObjToString(this object thisValue, string errorValue)
  63. {
  64. if (thisValue != null) return thisValue.ToString().Trim();
  65. return errorValue;
  66. }
  67. public static Decimal ObjToDecimal(this object thisValue)
  68. {
  69. Decimal reval = 0;
  70. if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
  71. {
  72. return reval;
  73. }
  74. return 0;
  75. }
  76. public static Decimal ObjToDecimal(this object thisValue, decimal errorValue)
  77. {
  78. Decimal reval = 0;
  79. if (thisValue != null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out reval))
  80. {
  81. return reval;
  82. }
  83. return errorValue;
  84. }
  85. public static DateTime ObjToDate(this object thisValue)
  86. {
  87. DateTime reval = DateTime.MinValue;
  88. if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
  89. {
  90. reval = Convert.ToDateTime(thisValue);
  91. }
  92. return reval;
  93. }
  94. public static DateTime ObjToDate(this object thisValue, DateTime errorValue)
  95. {
  96. DateTime reval = DateTime.MinValue;
  97. if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reval))
  98. {
  99. return reval;
  100. }
  101. return errorValue;
  102. }
  103. public static bool ObjToBool(this object thisValue)
  104. {
  105. bool reval = false;
  106. if (thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out reval))
  107. {
  108. return reval;
  109. }
  110. return reval;
  111. }
  112. }
  113. }