private static Query <T> BuildNotQuery <T>(string methodName, QueryBuilderExpression <T, string> queryBuilderExpression, string other) { var expression = CreateExpression(methodName, queryBuilderExpression, other); var lambda = Expression.Lambda <Func <T, bool> >(Expression.Not(expression), queryBuilderExpression._expression.Parameters); return(queryBuilderExpression._continueWith(new Query <T>(lambda))); }
private static Query <T> BuildQuery <T>(ConstantExpression otherEntity, QueryBuilderExpression <T, bool> queryBuilderExpression) { var expression = Expression.Equal(queryBuilderExpression._expression.Body, otherEntity); var lambda = Expression.Lambda <Func <T, bool> >(expression, queryBuilderExpression._expression.Parameters); return(queryBuilderExpression._continueWith(new Query <T>(lambda))); }
/// <summary> /// The property is not null or empty. /// </summary> /// <typeparam name="T">The type of object the Query is defined against.</typeparam> /// <param name="queryBuilderExpression">The Query.</param> /// <returns>True if the property is not null or empty, false otherwise.</returns> public static Query <T> NotNullOrEmpty <T>(this QueryBuilderExpression <T, string> queryBuilderExpression) { var method = typeof(string).GetMethod("IsNullOrEmpty", BindingFlags.Public | BindingFlags.Static); var expression = Expression.Call(method, queryBuilderExpression._expression.Body); var lambda = Expression.Lambda <Func <T, bool> >(Expression.Not(expression), queryBuilderExpression._expression.Parameters); return(queryBuilderExpression._continueWith(new Query <T>(lambda))); }
private static MethodCallExpression CreateExpression <T>(string methodName, QueryBuilderExpression <T, string> queryBuilderExpression, string other) { var otherEntity = Expression.Constant(other, typeof(string)); var method = typeof(string).GetMethod(methodName, new[] { typeof(string) }); var expression = Expression.Call(queryBuilderExpression._expression.Body, method, otherEntity); return(expression); }
/// <summary> /// The property contains at least one value. /// </summary> /// <typeparam name="T">The type of object the Query is defined against.</typeparam> /// <typeparam name="TEnumerable">The enumerable type (to support types inheriting IEnumerable).</typeparam> /// <typeparam name="T1">The type of the enumerable</typeparam> /// <param name="queryBuilderExpression">The Query.</param> /// <returns>True if the property contains at least one value, false otherwise.</returns> public static Query <T> NotEmpty <T, TEnumerable, T1>(this QueryBuilderExpression <T, TEnumerable> queryBuilderExpression) where TEnumerable : IEnumerable <T1> { var method = typeof(Enumerable).GetMethods().FirstOrDefault(m => m.Name == "Any" && m.GetParameters().Length == 1)?.MakeGenericMethod(typeof(T1)); var expression = Expression.Call(method, queryBuilderExpression._expression.Body); var lambda = Expression.Lambda <Func <T, bool> >(expression, queryBuilderExpression._expression.Parameters); return(queryBuilderExpression._continueWith(new Query <T>(lambda))); }
/// <summary> /// The property does not contain the same values in the same order as the supplied enumerable. /// </summary> /// <typeparam name="T">The type of object the Query is defined against.</typeparam> /// <typeparam name="TEnumerable">The enumerable type (to support types inheriting IEnumerable).</typeparam> /// <typeparam name="T1">The type of the enumerable</typeparam> /// <param name="queryBuilderExpression">The Query.</param> /// <param name="other">The enumerable to compare the property to.</param> /// <returns>True if the property does not contain the same elements in the same order as the supplied enumerable, false otherwise</returns> public static Query <T> NotEqualToSequence <T, TEnumerable, T1>(this QueryBuilderExpression <T, TEnumerable> queryBuilderExpression, IEnumerable <T1> other) where TEnumerable : IEnumerable <T1> { var otherEntity = Expression.Constant(other, typeof(IEnumerable <T1>)); var method = typeof(Enumerable).GetMethods().FirstOrDefault(m => m.Name == "SequenceEqual" && m.GetParameters().Length == 2)?.MakeGenericMethod(typeof(T1)); var expression = Expression.Call(method, queryBuilderExpression._expression.Body, otherEntity); var lambda = Expression.Lambda <Func <T, bool> >(Expression.Not(expression), queryBuilderExpression._expression.Parameters); return(queryBuilderExpression._continueWith(new Query <T>(lambda))); }
/// <summary> /// The property has at least one value not satisfying the supplied function. /// </summary> /// <typeparam name="T">The type of object the Query is defined against.</typeparam> /// <typeparam name="TEnumerable">The enumerable type (to support types inheriting IEnumerable).</typeparam> /// <typeparam name="T1">The type of the enumerable</typeparam> /// <param name="queryBuilderExpression">The Query.</param> /// <param name="func">The function to test the values of the enumerable property against.</param> /// <returns>True if the property contains at least one value that does not satisfy the supplied function. false otherwise.</returns> public static Query <T> WithNotAll <T, TEnumerable, T1>(this QueryBuilderExpression <T, TEnumerable> queryBuilderExpression, Expression <Func <T1, bool> > func) where TEnumerable : IEnumerable <T1> { var convertToQueryable = typeof(Queryable).GetMethods().FirstOrDefault(m => m.Name == "AsQueryable" && m.GetParameters().Length == 1)?.MakeGenericMethod(typeof(T1)); var queryableExpression = Expression.Call(convertToQueryable, queryBuilderExpression._expression.Body); var method = typeof(Queryable).GetMethods().FirstOrDefault(m => m.Name == "All" && m.GetParameters().Length == 2)?.MakeGenericMethod(typeof(T1)); var expression = Expression.Call(method, queryableExpression, func); var lambda = Expression.Lambda <Func <T, bool> >(Expression.Not(expression), queryBuilderExpression._expression.Parameters); return(queryBuilderExpression._continueWith(new Query <T>(lambda))); }
/// <summary> /// The property has at least one value not satisfying the supplied function. /// </summary> /// <typeparam name="T">The type of object the Query is defined against.</typeparam> /// <typeparam name="T1">The type of the enumerable</typeparam> /// <param name="queryBuilderExpression">The Query.</param> /// <param name="func">The function to test the values of the enumerable property against.</param> /// <returns>True if the property contains at least one value that does not satisfy the supplied function. false otherwise.</returns> public static Query <T> WithNotAll <T, T1>(this QueryBuilderExpression <T, List <T1> > queryBuilderExpression, Expression <Func <T1, bool> > func) { return(WithNotAll <T, List <T1>, T1>(queryBuilderExpression, func)); }
/// <summary> /// The property has all values satisfying the supplied function. /// </summary> /// <typeparam name="T">The type of object the Query is defined against.</typeparam> /// <typeparam name="T1">The type of the enumerable</typeparam> /// <param name="queryBuilderExpression">The Query.</param> /// <param name="func">The function to test the values of the enumerable property against.</param> /// <returns>True if the property has every value satisfying the supplied function, false otherwise.</returns> public static Query <T> WithAll <T, T1>(this QueryBuilderExpression <T, IReadOnlyCollection <T1> > queryBuilderExpression, Expression <Func <T1, bool> > func) { return(WithAll <T, IReadOnlyCollection <T1>, T1>(queryBuilderExpression, func)); }
/// <summary> /// The property has all values satisfying the supplied function. /// </summary> /// <typeparam name="T">The type of object the Query is defined against.</typeparam> /// <typeparam name="T1">The type of the enumerable</typeparam> /// <param name="queryBuilderExpression">The Query.</param> /// <param name="func">The function to test the values of the enumerable property against.</param> /// <returns>True if the property has every value satisfying the supplied function, false otherwise.</returns> public static Query <T> WithAll <T, T1>(this QueryBuilderExpression <T, IEnumerable <T1> > queryBuilderExpression, Expression <Func <T1, bool> > func) { return(WithAll <T, IEnumerable <T1>, T1>(queryBuilderExpression, func)); }
/// <summary> /// The property contains no values. /// </summary> /// <typeparam name="T">The type of object the Query is defined against.</typeparam> /// <typeparam name="T1">The type of the enumerable</typeparam> /// <param name="queryBuilderExpression">The Query.</param> /// <returns>True if the property contains no values, false otherwise.</returns> public static Query <T> Empty <T, T1>(this QueryBuilderExpression <T, IReadOnlyCollection <T1> > queryBuilderExpression) { return(Empty <T, IReadOnlyCollection <T1>, T1>(queryBuilderExpression)); }
/// <summary> /// The property is false. /// </summary> /// <typeparam name="T">The type of object the Query is defined against.</typeparam> /// <param name="queryBuilderExpression">The Query.</param> /// <returns>True if property is false, false otherwise.</returns> public static Query <T> False <T>(this QueryBuilderExpression <T, bool> queryBuilderExpression) => BuildQuery(Expression.Constant(false, typeof(bool)), queryBuilderExpression);
/// <summary> /// The property does not end with the supplied string. /// </summary> /// <typeparam name="T">The type of object the Query is defined against.</typeparam> /// <param name="queryBuilderExpression">The Query.</param> /// <param name="other">The value to test the property does not end with.</param> /// <returns>True if the property does not end with the supplied value, false otherwise.</returns> public static Query <T> NotEndingWith <T>(this QueryBuilderExpression <T, string> queryBuilderExpression, string other) => BuildNotQuery("EndsWith", queryBuilderExpression, other);
/// <summary> /// The property starts with the supplied string. /// </summary> /// <typeparam name="T">The type of object the Query is defined against.</typeparam> /// <param name="queryBuilderExpression">The Query.</param> /// <param name="other">The value to test the property starts with.</param> /// <returns>True if the property starts with the supplied value, false otherwise.</returns> public static Query <T> StartingWith <T>(this QueryBuilderExpression <T, string> queryBuilderExpression, string other) => BuildQuery("StartsWith", queryBuilderExpression, other);
/// <summary> /// The property does not contain the supplied string. /// </summary> /// <typeparam name="T">The type of object the Query is defined against.</typeparam> /// <param name="queryBuilderExpression">The Query.</param> /// <param name="other">The value to test the property does not contain.</param> /// <returns>True if the property does not contain the supplied value, false otherwise.</returns> public static Query <T> NotContaining <T>(this QueryBuilderExpression <T, string> queryBuilderExpression, string other) => BuildNotQuery("Contains", queryBuilderExpression, other);
/// <summary> /// The property contains at least one value. /// </summary> /// <typeparam name="T">The type of object the Query is defined against.</typeparam> /// <typeparam name="T1">The type of the enumerable</typeparam> /// <param name="queryBuilderExpression">The Query.</param> /// <returns>True if the property contains at least one value, false otherwise.</returns> public static Query <T> NotEmpty <T, T1>(this QueryBuilderExpression <T, IList <T1> > queryBuilderExpression) { return(NotEmpty <T, IList <T1>, T1>(queryBuilderExpression)); }
/// <summary> /// The property contains no values. /// </summary> /// <typeparam name="T">The type of object the Query is defined against.</typeparam> /// <typeparam name="T1">The type of the enumerable</typeparam> /// <param name="queryBuilderExpression">The Query.</param> /// <returns>True if the property contains no values, false otherwise.</returns> public static Query <T> Empty <T, T1>(this QueryBuilderExpression <T, IEnumerable <T1> > queryBuilderExpression) { return(Empty <T, IEnumerable <T1>, T1>(queryBuilderExpression)); }