SearchUtils.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. namespace comal.timesheets.Data_Classes
  6. {
  7. public static class SearchUtils
  8. {
  9. public static String UpperCaseFirst(string s)
  10. {
  11. char[] a = s.ToCharArray();
  12. a[0] = char.ToUpper(a[0]);
  13. return new string(a);
  14. }
  15. public static String LowerCaseFirst(string s)
  16. {
  17. char[] a = s.ToCharArray();
  18. a[0] = char.ToLower(a[0]);
  19. return new string(a);
  20. }
  21. public static String UpperCaseSecond(string s)
  22. {
  23. char[] a = s.ToCharArray();
  24. if (a.Length >= 2)
  25. {
  26. a[0] = char.ToUpper(a[0]);
  27. a[1] = char.ToUpper(a[1]);
  28. return new string(a);
  29. }
  30. else
  31. return s;
  32. }
  33. public static String UpperCaseThird(string s)
  34. {
  35. char[] a = s.ToCharArray();
  36. if (a.Length >= 3)
  37. {
  38. a[0] = char.ToUpper(a[0]);
  39. a[1] = char.ToUpper(a[1]);
  40. a[2] = char.ToUpper(a[2]);
  41. return new string(a);
  42. }
  43. else
  44. return s;
  45. }
  46. public static String UpperCaseFourth(string s)
  47. {
  48. char[] a = s.ToCharArray();
  49. if (a.Length >= 4)
  50. {
  51. a[0] = char.ToUpper(a[0]);
  52. a[1] = char.ToUpper(a[1]);
  53. a[2] = char.ToUpper(a[2]);
  54. a[3] = char.ToUpper(a[3]);
  55. return new string(a);
  56. }
  57. else
  58. return s;
  59. }
  60. #region DataGrid Sorting
  61. public enum SortDirection
  62. {
  63. Default,
  64. Ascending,
  65. Descending
  66. }
  67. public static IOrderedEnumerable<T> OrderByPropertyName<T>
  68. (
  69. this IEnumerable<T> items,
  70. string propertyName,
  71. SortDirection sortDirection = SortDirection.Ascending
  72. )
  73. {
  74. var propInfo = typeof(T).GetProperty(propertyName);
  75. object found = FindObjectWithPopulatedProperty(items, propInfo);
  76. if (DateTime.TryParse(propInfo.GetValue(found) as string, out DateTime date))
  77. return SortByDate(items, propInfo, sortDirection);
  78. else if (int.TryParse(propInfo.GetValue(found) as string, out int number))
  79. return SortByInt(items, propInfo, sortDirection);
  80. else if(double.TryParse(propInfo.GetValue(found) as string, out double dble))
  81. return SortByDouble(items, propInfo, sortDirection);
  82. else
  83. return items.OrderByDirection(x => propInfo.GetValue(x, null), sortDirection);
  84. }
  85. private static object FindObjectWithPopulatedProperty<T>(IEnumerable<T> items, PropertyInfo propInfo)
  86. {
  87. foreach (var item in items)
  88. {
  89. if (!string.IsNullOrWhiteSpace(propInfo.GetValue(item) as string))
  90. {
  91. return item;
  92. }
  93. }
  94. return items.FirstOrDefault();
  95. }
  96. public static IOrderedEnumerable<T> SortByDate<T>(IEnumerable<T> items, PropertyInfo propInfo, SortDirection sortDirection)
  97. {
  98. foreach (var item in items)
  99. {
  100. if (DateTime.TryParse(propInfo.GetValue(item) as string, out DateTime date))
  101. {
  102. var viewitem = item as DataGridViewModelItem;
  103. viewitem.SortDate = date;
  104. }
  105. else
  106. {
  107. var viewitem = item as DataGridViewModelItem;
  108. viewitem.SortDate = DateTime.MinValue;
  109. }
  110. }
  111. return items.OrderByDirection(x => typeof(T).GetProperty("SortDate").GetValue(x, null), sortDirection);
  112. }
  113. public static IOrderedEnumerable<T> SortByInt<T>(IEnumerable<T> items, PropertyInfo propInfo, SortDirection sortDirection)
  114. {
  115. foreach (var item in items)
  116. {
  117. if (int.TryParse(propInfo.GetValue(item) as string, out int sortint))
  118. {
  119. var viewitem = item as DataGridViewModelItem;
  120. viewitem.SortInt = sortint;
  121. }
  122. else
  123. {
  124. var viewitem = item as DataGridViewModelItem;
  125. viewitem.SortInt = 0;
  126. }
  127. }
  128. return items.OrderByDirection(x => typeof(T).GetProperty("SortInt").GetValue(x, null), sortDirection);
  129. }
  130. public static IOrderedEnumerable<T> SortByDouble<T>(IEnumerable<T> items, PropertyInfo propInfo, SortDirection sortDirection)
  131. {
  132. foreach (var item in items)
  133. {
  134. if (double.TryParse(propInfo.GetValue(item) as string, out double dble))
  135. {
  136. var viewitem = item as DataGridViewModelItem;
  137. viewitem.SortDouble = dble;
  138. }
  139. else
  140. {
  141. var viewitem = item as DataGridViewModelItem;
  142. viewitem.SortDouble = 0;
  143. }
  144. }
  145. return items.OrderByDirection(x => typeof(T).GetProperty("SortInt").GetValue(x, null), sortDirection);
  146. }
  147. public static IOrderedEnumerable<T> OrderByDirection<T, TKey>
  148. (
  149. this IEnumerable<T> items,
  150. Func<T, TKey> keyExpression,
  151. SortDirection sortDirection = SortDirection.Ascending
  152. )
  153. {
  154. switch (sortDirection)
  155. {
  156. case SortDirection.Ascending:
  157. return items.OrderBy(keyExpression);
  158. case SortDirection.Descending:
  159. return items.OrderByDescending(keyExpression);
  160. default:
  161. return items.OrderBy(keyExpression);
  162. }
  163. }
  164. #endregion
  165. }
  166. }