public bool CompareNumeric(object value, Type targetType, object parameter, CultureInfo culture) { CompareOperation op; object cv; if (Operation == CompareOperation.InParameter) { var p = ((string)parameter).Split(new char[] { ' ' }, 2); var ops = p[0]; if (p[1].ToLowerInvariant() == "null" || p[1].ToLowerInvariant() == "{null}") { ops = p[0] + " null"; //TODO nullable numeric? } cv = p[1]; switch (ops.ToLowerInvariant()) { case "eq": op = CompareOperation.Equal; break; case "gt": op = CompareOperation.GreaterThean; break; case "lt": op = CompareOperation.LessThen; break; case "ge": op = CompareOperation.GreaterTheanOrEqual; break; case "le": op = CompareOperation.LessThenOrEqual; break; case "ne": op = CompareOperation.NotEqual; break; default: op = CompareOperation.None; break; } } else { op = Operation; cv = parameter; } var v0 = System.Convert.ToDouble(value); var v1 = (double)DoubleConverter.ConvertFrom(cv); switch (op) { case CompareOperation.Equal: return(v0 == v1); case CompareOperation.GreaterThean: return(v0 > v1); case CompareOperation.LessThen: return(v0 < v1); case CompareOperation.GreaterTheanOrEqual: return(v0 >= v1); case CompareOperation.LessThenOrEqual: return(v0 <= v1); case CompareOperation.NotEqual: return(v0 != v1); } return(false); }
/// <summary> /// Computes an average for all values in an enumerable, ensuring they can be converted to double (using a <see cref="DoubleConverter"/>). /// </summary> /// <typeparam name="T">Items type</typeparam> /// <param name="src">Source enumeration</param> /// <param name="ignoreErrors">Specify whether errors are blocking</param> /// <returns>The computed average</returns> public static double AverageChecked <T>(this IEnumerable <T> src, bool ignoreErrors = false) where T : IConvertible { Contract.Requires(src != null); var converter = new DoubleConverter(); var s = src; if (ignoreErrors) { s = s.Where(c => converter.IsValid(c)); } return(src.Average(x => (double)converter.ConvertFrom(x))); }