/// <summary> /// Create an expression /// </summary> /// <param name="type">Type expression to create</param> /// <param name="parameter">Parameter to will be used to make an expression</param> /// <param name="prop">Property to will be used to make an expression</param> /// <param name="value">Value to will be used to make an expression</param> /// <returns></returns> private static Expression SetExpressionType( this LambdaMethod type, ParameterExpression parameter, PropertyInfo prop, object value) { Expression lambda = null; switch (type) { case LambdaMethod.Contains: if (prop.PropertyType.IsString(nameof(SetExpressionType), prop.Name)) { MethodInfo method = typeof(string).GetMethod(LambdaMethod.Contains.ToString(), new[] { typeof(string) }); lambda = Expression.Call(Expression.Property(parameter, prop), method, Expression.Constant(value)); } break; case LambdaMethod.GreaterThan: if (prop.GetType().IsNotString(nameof(SetExpressionType), prop.Name, LambdaMethod.GreaterThan.ToString())) { lambda = Expression.GreaterThan(Expression.Property(parameter, prop), Expression.Constant(value)); } break; case LambdaMethod.LessThan: if (prop.GetType() .IsNotString( nameof(SetExpressionType), prop.Name, LambdaMethod.LessThan.ToString())) { lambda = Expression.LessThan(Expression.Property(parameter, prop), Expression.Constant(value)); } break; case LambdaMethod.GreaterThanOrEqual: if (prop.GetType().IsNotString(nameof(SetExpressionType), prop.Name, LambdaMethod.GreaterThanOrEqual.ToString())) { lambda = Expression.GreaterThanOrEqual(Expression.Property(parameter, prop), Expression.Constant(value)); } break; case LambdaMethod.LessThanOrEqual: if (prop.GetType().IsNotString(nameof(SetExpressionType), prop.Name, LambdaMethod.GreaterThanOrEqual.ToString())) { lambda = Expression.LessThanOrEqual(Expression.Property(parameter, prop), Expression.Constant(value)); } break; default: return(Expression.Equal(Expression.Property(parameter, prop), Expression.Constant(value))); } return(lambda); }
/// <summary> /// transforms an enumeration using a selector /// </summary> /// <param name="enumeration">enumeration to transform</param> /// <param name="selector">selector to use to transform selection</param> /// <returns>resulting enumeration</returns> public static IEnumerable SelectMany(IEnumerable enumeration, LambdaMethod selector) { return(enumeration.Cast <object>().SelectMany(i => { object value = selector.Invoke(i); if (value is IEnumerable selection) { return selection.Cast <object>(); } return new[] { value }; })); }
/// <summary>Creates the type of the expression per.</summary> /// <param name="type">The type.</param> /// <param name="parameter">The parameter.</param> /// <param name="propertyInfo">The property information.</param> /// <param name="value">The value.</param> /// <returns></returns> private static Expression CreateExpressionPerType( this LambdaMethod type, ParameterExpression parameter, PropertyInfo propertyInfo, object value) { ThrowErrorIf.IsNullValue(parameter, nameof(parameter), nameof(CreateExpressionPerType)); ThrowErrorIf.IsNullValue(propertyInfo, nameof(propertyInfo), nameof(CreateExpressionPerType)); var memberExpression = Expression.Property(parameter, propertyInfo); var constant = Expression.Constant(value); switch (type) { case LambdaMethod.Contains: var result = FilterFacade.Contains(constant, memberExpression, value); return(result); case LambdaMethod.GreaterThan: result = FilterFacade.GreaterThan(constant, memberExpression, value); return(result); case LambdaMethod.LessThan: result = FilterFacade.LessThan(constant, memberExpression, value); return(result); case LambdaMethod.GreaterThanOrEqual: result = FilterFacade.GreaterThanOrEqual(constant, memberExpression, value); return(result); case LambdaMethod.LessThanOrEqual: result = FilterFacade.LessThanOrEqual(constant, memberExpression, value); return(result); default: result = FilterFacade.Equal(constant, memberExpression, value); return(result); } }
/// <summary> /// get first item of enumeration which matches the predicate or null if no element matches the predicate /// </summary> /// <param name="enumeration">enumeration of which to return first element</param> /// <param name="predicate">predicate for element to match</param> /// <returns>first element of enumeration which matches the predicate or null if no element matches</returns> public static object FirstOrDefault(IEnumerable enumeration, LambdaMethod predicate) { return(enumeration.Cast <object>().FirstOrDefault(i => predicate.Invoke(i).ToBoolean())); }
/// <summary> /// filters an enumeration using a predicate /// </summary> /// <param name="enumeration">enumeration to filter</param> /// <param name="predicate">filter predicate</param> /// <returns>filtered enumeration</returns> public static IEnumerable Where(IEnumerable enumeration, LambdaMethod predicate) { return(enumeration.Cast <object>().Where(i => predicate.Invoke(i).ToBoolean())); }
/// <summary> /// transforms an enumeration using a selector /// </summary> /// <param name="enumeration">enumeration to transform</param> /// <param name="selector">selector to use to transform selection</param> /// <returns>resulting enumeration</returns> public static IEnumerable Select(IEnumerable enumeration, LambdaMethod selector) { return(enumeration.Cast <object>().Select(i => selector.Invoke(i))); }
/// <summary> /// determines whether any item in the collection matches the specified predicate /// </summary> /// <param name="collection">collection to analyse</param> /// <param name="predicate">predicate to execute</param> /// <returns>true if any item matches the predicate, false otherwise</returns> public static bool All(IEnumerable collection, LambdaMethod predicate) { return(collection.Cast <object>().All(i => predicate.Invoke(i).ToBoolean())); }