Admin import functional
This commit is contained in:
@@ -3,9 +3,11 @@ using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Data.SqlTypes;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Web.Mvc;
|
||||
using MileageTraker.Web.Models;
|
||||
|
||||
namespace MileageTraker.Web.Utility
|
||||
{
|
||||
@@ -61,6 +63,47 @@ namespace MileageTraker.Web.Utility
|
||||
});
|
||||
}
|
||||
|
||||
public static Enum ParseWithDisplayNames(this Type enumType, string item)
|
||||
{
|
||||
Func<string, bool> f = s => string.Equals(s, item, StringComparison.CurrentCultureIgnoreCase);
|
||||
return Enum.GetValues(enumType)
|
||||
.OfType<Enum>().First(e => e.GetAllNameVariants().Any(f));
|
||||
}
|
||||
|
||||
public static bool IsValidValue(this Type enumType, string item)
|
||||
{
|
||||
Func<string, bool> f = s => string.Equals(s, item, StringComparison.CurrentCultureIgnoreCase);
|
||||
return Enum.GetValues(enumType)
|
||||
.OfType<Enum>().Any(e => e.GetAllNameVariants().Any(f));
|
||||
}
|
||||
|
||||
public static IEnumerable<string> GetAllNameVariants(this Enum enumeration)
|
||||
{
|
||||
yield return enumeration.ToString();
|
||||
yield return enumeration.GetDisplayName();
|
||||
yield return enumeration.GetDisplayShortName();
|
||||
}
|
||||
|
||||
public static Expression<Func<Log, bool>> GetEqualsPredicate(this MileageLogType mileLogType)
|
||||
{
|
||||
Expression<Func<Log, bool>> predicate;
|
||||
switch (mileLogType)
|
||||
{
|
||||
case MileageLogType.NonCommuting:
|
||||
predicate = l => l.LogType.Value == 1;
|
||||
break;
|
||||
case MileageLogType.Commuting:
|
||||
predicate = l => l.LogType.Value == 2;
|
||||
break;
|
||||
case MileageLogType.GasPurchase:
|
||||
predicate = l => l.LogType.Value == 3;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
return predicate;
|
||||
}
|
||||
|
||||
public static IEnumerable<SelectListItem> ToSelectList(this Type enumType, string selectedItem, string noItemSelected)
|
||||
{
|
||||
var items = (from item in Enum.GetValues(enumType).OfType<Enum>()
|
||||
@@ -99,6 +142,13 @@ namespace MileageTraker.Web.Utility
|
||||
return value;
|
||||
}
|
||||
|
||||
public static IEnumerable<string> GetPropertyNames(this Type type)
|
||||
{
|
||||
return
|
||||
from property in type.GetProperties()
|
||||
select property.GetName();
|
||||
}
|
||||
|
||||
public static bool IsSqlMinValue(this DateTime dt)
|
||||
{
|
||||
return dt == SqlDateTime.MinValue.Value;
|
||||
|
||||
Reference in New Issue
Block a user