Improved filtering implemented.

This commit is contained in:
2014-05-16 18:23:18 -04:00
parent b2d57a9e5f
commit fe0bc4e9ff
14 changed files with 425 additions and 133 deletions
+113 -45
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Data.SqlTypes;
using System.Linq;
@@ -7,7 +8,9 @@ using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using MileageTraker.Web.Models;
using MiscUtil.Collections;
namespace MileageTraker.Web.Utility
{
@@ -16,9 +19,9 @@ namespace MileageTraker.Web.Utility
public static string Wordify(this string str)
{
return str.Aggregate(
String.Empty,
(current, c) =>
current + (Char.IsUpper(c) ? " " + c : c.ToString()));
String.Empty,
(current, c) =>
current + (Char.IsUpper(c) ? " " + c : c.ToString()));
}
private static T GetAttribute<T>(this Enum enumeration) where T : Attribute
@@ -44,23 +47,23 @@ namespace MileageTraker.Web.Utility
{
var displayAttribute = GetAttribute<DisplayAttribute>(enumeration);
return displayAttribute != null
? (!String.IsNullOrEmpty(displayAttribute.ShortName)
? displayAttribute.ShortName
: displayAttribute.Name)
: enumeration.ToString();
? (!String.IsNullOrEmpty(displayAttribute.ShortName)
? displayAttribute.ShortName
: displayAttribute.Name)
: enumeration.ToString();
}
public static IEnumerable<SelectListItem> GetSelectListItems(this Enum enumeration)
{
var type = enumeration.GetType();
return Enum.GetValues(type)
.OfType<Enum>().Select(e =>
new SelectListItem
{
Text = e.GetDisplayName(),
Value = e.ToString(),
Selected = e.Equals(enumeration)
});
.OfType<Enum>().Select(e =>
new SelectListItem
{
Text = e.GetDisplayName(),
Value = e.ToString(),
Selected = e.Equals(enumeration)
});
}
public static Enum ParseWithDisplayNames(this Type enumType, string item)
@@ -74,7 +77,7 @@ namespace MileageTraker.Web.Utility
{
Func<string, bool> f = s => string.Equals(s, item, StringComparison.CurrentCultureIgnoreCase);
return Enum.GetValues(enumType)
.OfType<Enum>().Any(e => e.GetAllNameVariants().Any(f));
.OfType<Enum>().Any(e => e.GetAllNameVariants().Any(f));
}
public static IEnumerable<string> GetAllNameVariants(this Enum enumeration)
@@ -109,27 +112,29 @@ namespace MileageTraker.Web.Utility
var items = (from item in Enum.GetValues(enumType).OfType<Enum>()
let title = item.GetDisplayName()
select new SelectListItem
{
Value = item.ToString(),
Text = title,
Selected = selectedItem == item.ToString()
}).ToList();
{
Value = item.ToString(),
Text = title,
Selected = selectedItem == item.ToString()
}).ToList();
return new SelectList(new[]{new SelectListItem{Text = noItemSelected}}.Concat(items), "Value", "Text");
return new SelectList(new[] {new SelectListItem {Text = noItemSelected}}.Concat(items), "Value", "Text");
}
public static string GetName(this MemberInfo propertyMember)
{
var displayAttr = propertyMember.GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault() as DisplayAttribute;
var displayAttr =
propertyMember.GetCustomAttributes(typeof (DisplayAttribute), true).FirstOrDefault() as DisplayAttribute;
return displayAttr != null
? displayAttr.Name
: propertyMember.Name;
? displayAttr.Name
: propertyMember.Name;
}
public static object GetValue(this PropertyInfo propertyMember, object obj)
{
var value = propertyMember.GetValue(obj, null);
var displayAttr = propertyMember.GetCustomAttributes(typeof(DisplayFormatAttribute), true).FirstOrDefault() as DisplayFormatAttribute;
var displayAttr =
propertyMember.GetCustomAttributes(typeof (DisplayFormatAttribute), true).FirstOrDefault() as DisplayFormatAttribute;
if (displayAttr != null)
{
@@ -144,7 +149,7 @@ namespace MileageTraker.Web.Utility
public static IEnumerable<string> GetPropertyNames(this Type type)
{
return
return
from property in type.GetProperties()
select property.GetName();
}
@@ -164,34 +169,34 @@ namespace MileageTraker.Web.Utility
var sb = new StringBuilder();
Action<int, string> b = (var, name) =>
{
if (var > 0)
{
if (sb.Length > 0)
sb.Append(", ");
sb.Append(var + " " + name);
if (var > 1)
sb.Append("s");
}
};
if (var > 0)
{
if (sb.Length > 0)
sb.Append(", ");
sb.Append(var + " " + name);
if (var > 1)
sb.Append("s");
}
};
var timeLeft = ts;
var years = timeLeft.Days / 365; //no leap year accounting
var years = timeLeft.Days/365; //no leap year accounting
b(years, "year");
timeLeft -= new TimeSpan(years*365, 0, 0, 0);
var months = timeLeft.Days / 30; //naive guess at month size
b(months, "month");
timeLeft -= new TimeSpan(months * 30, 0, 0, 0);
var weeks = timeLeft.Days / 7;
var months = timeLeft.Days/30; //naive guess at month size
b(months, "month");
timeLeft -= new TimeSpan(months*30, 0, 0, 0);
var weeks = timeLeft.Days/7;
b(weeks, "week");
timeLeft -= new TimeSpan(weeks * 7, 0, 0, 0);
timeLeft -= new TimeSpan(weeks*7, 0, 0, 0);
var days = timeLeft.Days;
b(days, "day");
sb.Append(" ago");
return sb.ToString();
}
@@ -220,7 +225,7 @@ namespace MileageTraker.Web.Utility
var i = 0;
while (true) yield return i++;
}
/// <summary>
/// Users with activity date greater than this value are online
/// </summary>
@@ -231,5 +236,68 @@ namespace MileageTraker.Web.Utility
Convert.ToDouble(
System.Web.Security.Membership.UserIsOnlineTimeWindow)));
}
public static Dictionary<string, List<string>> YearMonthList(IEnumerable<DateTime> dates)
{
return
(from d in dates
orderby d.Year descending
group d by d.Year
into yg
select
new
{
Year = yg.Key.ToString(),
MonthList =
(from o in yg
orderby o.Month descending
group o by o.Month
into mg
select mg.Key.ToString()).ToList()
}).ToDictionary(arg => arg.Year, arg => arg.MonthList);
}
public static RouteValueDictionary ToRouteValueDictionary(this NameValueCollection collection)
{
var routeValueDictionary = new RouteValueDictionary();
foreach (var key in collection.AllKeys)
{
routeValueDictionary.Add(key, collection[key]);
}
return routeValueDictionary;
}
//public static IEnumerable<SelectListItem> ToSelectList<T>(
// this IEnumerable<T> enumerable,
// Func<T, string> text,
// Func<T, string> value,
// string selectedValue = null,
// string defaultOption = null)
//{
// var items = enumerable.Select(f => new SelectListItem
// {
// Text = text(f),
// Value = value(f),
// Selected = value(f) == selectedValue,
// }).ToList();
// if (!string.IsNullOrEmpty(defaultOption))
// {
// items.Insert(0, new SelectListItem
// {
// Text = defaultOption,
// Value = "-1"
// });
// }
// return items;
//}
//public static IEnumerable<SelectListItem> ToSelectList<T>(
// this IEnumerable<T> enumerable,
// string selectedValue = null,
// string defaultOption = null)
//{
// return enumerable.ToSelectList(t => t.ToString(), t => t.ToString());
//}
}
}
+2 -2
View File
@@ -100,9 +100,9 @@ namespace MileageTraker.Web.Utility
formatString = "#,##0.00";
if (value is DateTime && p.Name == "Date")
formatString = @"YYYY\-MM\-DD";
formatString = @"MM/DD/YYYY";
else if (value is DateTime)
formatString = @"YYYY\-MM\-DD hh:mm:ss";
formatString = @"MM/DD/YYYY hh:mm:ss";
int intValue; // write int-looking values as numbers
if (value is string && int.TryParse((string) value, out intValue))