SearchUtils.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  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. public enum SortDirection
  61. {
  62. Default,
  63. Ascending,
  64. Descending
  65. }
  66. public static IOrderedEnumerable<T> OrderByPropertyName<T>
  67. (
  68. this IEnumerable<T> items,
  69. string propertyName,
  70. SortDirection sortDirection = SortDirection.Ascending
  71. )
  72. {
  73. var propInfo = typeof(T).GetProperty(propertyName);
  74. return items.OrderByDirection(x => propInfo.GetValue(x, null), sortDirection);
  75. }
  76. public static IOrderedEnumerable<T> OrderByDirection<T, TKey>
  77. (
  78. this IEnumerable<T> items,
  79. Func<T, TKey> keyExpression,
  80. SortDirection sortDirection = SortDirection.Ascending
  81. )
  82. {
  83. switch (sortDirection)
  84. {
  85. case SortDirection.Ascending:
  86. return items.OrderBy(keyExpression);
  87. case SortDirection.Descending:
  88. return items.OrderByDescending(keyExpression);
  89. default:
  90. return items.OrderBy(keyExpression);
  91. }
  92. }
  93. }
  94. }