| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- namespace comal.timesheets.Data_Classes
- {
- public static class SearchUtils
- {
- public static String UpperCaseFirst(string s)
- {
- char[] a = s.ToCharArray();
- a[0] = char.ToUpper(a[0]);
- return new string(a);
- }
- public static String LowerCaseFirst(string s)
- {
- char[] a = s.ToCharArray();
- a[0] = char.ToLower(a[0]);
- return new string(a);
- }
- public static String UpperCaseSecond(string s)
- {
- char[] a = s.ToCharArray();
- if (a.Length >= 2)
- {
- a[0] = char.ToUpper(a[0]);
- a[1] = char.ToUpper(a[1]);
- return new string(a);
- }
- else
- return s;
- }
- public static String UpperCaseThird(string s)
- {
- char[] a = s.ToCharArray();
- if (a.Length >= 3)
- {
- a[0] = char.ToUpper(a[0]);
- a[1] = char.ToUpper(a[1]);
- a[2] = char.ToUpper(a[2]);
- return new string(a);
- }
- else
- return s;
- }
-
- public static String UpperCaseFourth(string s)
- {
- char[] a = s.ToCharArray();
- if (a.Length >= 4)
- {
- a[0] = char.ToUpper(a[0]);
- a[1] = char.ToUpper(a[1]);
- a[2] = char.ToUpper(a[2]);
- a[3] = char.ToUpper(a[3]);
- return new string(a);
- }
- else
- return s;
- }
- #region DataGrid Sorting
- public enum SortDirection
- {
- Default,
- Ascending,
- Descending
- }
- public static IOrderedEnumerable<T> OrderByPropertyName<T>
- (
- this IEnumerable<T> items,
- string propertyName,
- SortDirection sortDirection = SortDirection.Ascending
- )
- {
- var propInfo = typeof(T).GetProperty(propertyName);
- object found = FindObjectWithPopulatedProperty(items, propInfo);
- if (DateTime.TryParse(propInfo.GetValue(found) as string, out DateTime date))
- return SortByDate(items, propInfo, sortDirection);
- else if (int.TryParse(propInfo.GetValue(found) as string, out int number))
- return SortByInt(items, propInfo, sortDirection);
- else if(double.TryParse(propInfo.GetValue(found) as string, out double dble))
- return SortByDouble(items, propInfo, sortDirection);
- else
- return items.OrderByDirection(x => propInfo.GetValue(x, null), sortDirection);
- }
- private static object FindObjectWithPopulatedProperty<T>(IEnumerable<T> items, PropertyInfo propInfo)
- {
- foreach (var item in items)
- {
- if (!string.IsNullOrWhiteSpace(propInfo.GetValue(item) as string))
- {
- return item;
- }
- }
- return items.FirstOrDefault();
- }
- public static IOrderedEnumerable<T> SortByDate<T>(IEnumerable<T> items, PropertyInfo propInfo, SortDirection sortDirection)
- {
- foreach (var item in items)
- {
- if (DateTime.TryParse(propInfo.GetValue(item) as string, out DateTime date))
- {
- var viewitem = item as DataGridViewModelItem;
- viewitem.SortDate = date;
- }
- else
- {
- var viewitem = item as DataGridViewModelItem;
- viewitem.SortDate = DateTime.MinValue;
- }
- }
- return items.OrderByDirection(x => typeof(T).GetProperty("SortDate").GetValue(x, null), sortDirection);
- }
- public static IOrderedEnumerable<T> SortByInt<T>(IEnumerable<T> items, PropertyInfo propInfo, SortDirection sortDirection)
- {
- foreach (var item in items)
- {
- if (int.TryParse(propInfo.GetValue(item) as string, out int sortint))
- {
- var viewitem = item as DataGridViewModelItem;
- viewitem.SortInt = sortint;
- }
- else
- {
- var viewitem = item as DataGridViewModelItem;
- viewitem.SortInt = 0;
- }
- }
- return items.OrderByDirection(x => typeof(T).GetProperty("SortInt").GetValue(x, null), sortDirection);
- }
- public static IOrderedEnumerable<T> SortByDouble<T>(IEnumerable<T> items, PropertyInfo propInfo, SortDirection sortDirection)
- {
- foreach (var item in items)
- {
- if (double.TryParse(propInfo.GetValue(item) as string, out double dble))
- {
- var viewitem = item as DataGridViewModelItem;
- viewitem.SortDouble = dble;
- }
- else
- {
- var viewitem = item as DataGridViewModelItem;
- viewitem.SortDouble = 0;
- }
- }
- return items.OrderByDirection(x => typeof(T).GetProperty("SortInt").GetValue(x, null), sortDirection);
- }
- public static IOrderedEnumerable<T> OrderByDirection<T, TKey>
- (
- this IEnumerable<T> items,
- Func<T, TKey> keyExpression,
- SortDirection sortDirection = SortDirection.Ascending
- )
- {
- switch (sortDirection)
- {
- case SortDirection.Ascending:
- return items.OrderBy(keyExpression);
- case SortDirection.Descending:
- return items.OrderByDescending(keyExpression);
- default:
- return items.OrderBy(keyExpression);
- }
- }
- #endregion
- }
- }
|