LinQExtensions.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace LYFZ
  6. {
  7. public static class LinQExtensions
  8. {
  9. public static string ToYYYY_MM_dd_HH_mm_ss(this DateTime dtime)
  10. {
  11. return dtime.ToString("yyyy-MM-dd HH:mm:ss");
  12. }
  13. public static string ToYYYY_MM_dd(this DateTime dtime)
  14. {
  15. return dtime.ToString("yyyy-MM-dd");
  16. }
  17. public static string ToHH_mm_ss(this DateTime dtime)
  18. {
  19. return dtime.ToString("HH:mm:ss");
  20. }
  21. /// <summary>
  22. /// 转对象为字符串 如果对象为null 返加 空字符串
  23. /// </summary>
  24. /// <param name="obj"></param>
  25. /// <returns></returns>
  26. public static string ToString_s(this object obj)
  27. {
  28. if (obj != null)
  29. {
  30. return obj.ToString();
  31. }
  32. else {
  33. return "";
  34. }
  35. }
  36. /// <summary>
  37. /// 转为小写字符串
  38. /// </summary>
  39. /// <param name="obj"></param>
  40. /// <returns></returns>
  41. public static string ToLowerString(this object obj)
  42. {
  43. return obj.ToString().ToLower();
  44. }
  45. /// <summary>
  46. /// 转为去除前后空格的小写字符串
  47. /// </summary>
  48. /// <param name="obj"></param>
  49. /// <returns></returns>
  50. public static string ToTrimLowerString(this object obj)
  51. {
  52. return obj.ToString().ToLower().Trim();
  53. }
  54. /// <summary>
  55. /// 转为大写字符串
  56. /// </summary>
  57. /// <param name="obj"></param>
  58. /// <returns></returns>
  59. public static string ToUpperString(this object obj)
  60. {
  61. return obj.ToString().ToUpper();
  62. }
  63. /// <summary>
  64. /// 转为去除前后空格的大写字符串
  65. /// </summary>
  66. /// <param name="obj"></param>
  67. /// <returns></returns>
  68. public static string ToTrimUpperString(this object obj)
  69. {
  70. return obj.ToString().ToUpper().Trim();
  71. }
  72. /// <summary>
  73. /// 通过使用默认的相等比较器对值进行比较返回序列中的非重复元素。
  74. /// </summary>
  75. /// <typeparam name="TSource"></typeparam>
  76. /// <typeparam name="TKey"></typeparam>
  77. /// <param name="source"></param>
  78. /// <param name="keySelector"></param>
  79. /// <returns></returns>
  80. public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  81. {
  82. HashSet<TKey> seenKeys = new HashSet<TKey>();
  83. foreach (TSource element in source)
  84. {
  85. if (seenKeys.Add(keySelector(element)))
  86. {
  87. yield return element;
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// 转 布尔值为 int
  93. /// </summary>
  94. /// <param name="bl"></param>
  95. /// <returns></returns>
  96. public static int BoolToInt(this bool bl)
  97. {
  98. return bl ? 1 : 0;
  99. }
  100. /// <summary>
  101. /// 转 int 为 布尔值
  102. /// </summary>
  103. /// <param name="i"></param>
  104. /// <returns></returns>
  105. public static bool IntToBool(this int i)
  106. {
  107. return i > 0;
  108. }
  109. /// <summary>
  110. /// DateTime转换为JavaScript时间戳 13位数长度
  111. /// </summary>
  112. /// <param name="t"></param>
  113. /// <returns></returns>
  114. public static long ToJavaScriptTimeStamp(this DateTime t)
  115. {
  116. return LYFZ.WinAPI.TimeStampConversion.DateTimeToJavaScriptTimeStamp(t);
  117. }
  118. /// <summary>
  119. /// JavaScript时间戳转换为DateTime
  120. /// </summary>
  121. /// <param name="timeStamp">为有效的时间戳整数</param>
  122. /// <returns></returns>
  123. public static DateTime JavaScriptTimeStampToDateTime(this long timeStamp)
  124. {
  125. try
  126. {
  127. return LYFZ.WinAPI.TimeStampConversion.JavaScriptTimeStampToDateTime(timeStamp);
  128. }
  129. catch (Exception ex)
  130. {
  131. throw new Exception("时间戳转换时出错,时间戳无效:" + ex.Message);
  132. }
  133. }
  134. /// <summary>
  135. /// 序列化object到byte[]
  136. /// </summary>
  137. /// <param name="o"></param>
  138. /// <returns></returns>
  139. public static byte[] ToSerializerRetBytes(this object o)
  140. {
  141. if (o != null)
  142. {
  143. return LYFZ.WinAPI.DataSetSerializerDeserialize.ObjectSerializerRetBytes(o);
  144. }
  145. else
  146. {
  147. return null;
  148. }
  149. }
  150. }
  151. }