From 18a3756f515fd00685604259a8b818779347175a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Leb=C3=A9e?= Date: Wed, 14 Mar 2018 19:25:47 -0500 Subject: [PATCH] got rid of generic depedancy. :) --- .../Extensions/DbContextExtensions.cs | 22 ++-- .../ComplexQueriesTests.cs | 4 +- .../Extensions/EnumerableExtensions.cs | 4 +- .../Extensions/QueryableExtensions.cs | 8 +- .../Fluent/WhereBuilder/WhereBuilder.cs | 100 +++++++++++++----- ...shortcuts.cs => WhereBuilder.shortcuts.cs} | 76 ++++++------- .../Fluent/WhereBuilder/WhereBuilderBase.cs | 61 ----------- .../Helpers/QueryableHelpers.cs | 41 +++++-- 8 files changed, 160 insertions(+), 156 deletions(-) rename PoweredSoft.DynamicLinq/Fluent/WhereBuilder/{WhereBuilderBase.shortcuts.cs => WhereBuilder.shortcuts.cs} (53%) delete mode 100644 PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilderBase.cs diff --git a/PoweredSoft.DynamicLinq.EntityFramework/Extensions/DbContextExtensions.cs b/PoweredSoft.DynamicLinq.EntityFramework/Extensions/DbContextExtensions.cs index daab0e4..71e8632 100644 --- a/PoweredSoft.DynamicLinq.EntityFramework/Extensions/DbContextExtensions.cs +++ b/PoweredSoft.DynamicLinq.EntityFramework/Extensions/DbContextExtensions.cs @@ -11,19 +11,17 @@ namespace PoweredSoft.DynamicLinq.EntityFramework { public static class DbContextExtensions { - private static readonly MethodInfo QueryMethod = typeof(DbContextExtensions) - .GetMethods(BindingFlags.Static | BindingFlags.Public) - .First(t => t.Name == "Query" && t.IsGenericMethod); - - public static IQueryable Query(this DbContext context, Type pocoType, Action callback) + public static IQueryable Query(this DbContext context, Type pocoType, Action callback) { - var method = QueryMethod.MakeGenericMethod(pocoType); - var invokeResult = method.Invoke(null, new object[] {context, callback}); - var ret = invokeResult as IQueryable; - return ret; + var set = context.Set(pocoType); + var queryable = set.AsQueryable(); + var builder = new WhereBuilder(queryable); + callback(builder); + var result = builder.Build(); + return result; } - public static IQueryable Query(this DbContext context, Action callback) + public static IQueryable Query(this DbContext context, Action callback) where T : class { var query = context.Set().AsQueryable(); @@ -31,10 +29,10 @@ namespace PoweredSoft.DynamicLinq.EntityFramework return query; } - public static IQueryable Where(this DbContext context, Type pocoType, Action callback) + public static IQueryable Where(this DbContext context, Type pocoType, Action callback) => context.Query(pocoType, callback); - public static IQueryable Where(this DbContext context, Action callback) + public static IQueryable Where(this DbContext context, Action callback) where T : class => context.Query(callback); } } diff --git a/PoweredSoft.DynamicLinq.Test/ComplexQueriesTests.cs b/PoweredSoft.DynamicLinq.Test/ComplexQueriesTests.cs index ca6c48f..33809ca 100644 --- a/PoweredSoft.DynamicLinq.Test/ComplexQueriesTests.cs +++ b/PoweredSoft.DynamicLinq.Test/ComplexQueriesTests.cs @@ -56,7 +56,7 @@ namespace PoweredSoft.DynamicLinq.Test // the query. var query = posts.AsQueryable(); - var queryBuilder = new WhereBuilder(query); + var queryBuilder = new WhereBuilder(query); queryBuilder.Compare("AuthorId", ConditionOperators.Equal, 1); queryBuilder.And(subQuery => @@ -65,7 +65,7 @@ namespace PoweredSoft.DynamicLinq.Test subQuery.Or("Title", ConditionOperators.Contains, 3); }); - query = queryBuilder.Build(); + query = (IQueryable)queryBuilder.Build(); Assert.AreEqual(2, query.Count()); } diff --git a/PoweredSoft.DynamicLinq/Extensions/EnumerableExtensions.cs b/PoweredSoft.DynamicLinq/Extensions/EnumerableExtensions.cs index 47dfd0b..dab828a 100644 --- a/PoweredSoft.DynamicLinq/Extensions/EnumerableExtensions.cs +++ b/PoweredSoft.DynamicLinq/Extensions/EnumerableExtensions.cs @@ -14,10 +14,10 @@ namespace PoweredSoft.DynamicLinq QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => list.AsQueryable().Where(path, conditionOperator, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public static IEnumerable Where(this IEnumerable list, Action> callback) + public static IEnumerable Where(this IEnumerable list, Action callback) => list.Query(callback); - public static IEnumerable Query(this IEnumerable list, Action> callback) + public static IEnumerable Query(this IEnumerable list, Action callback) => list.AsQueryable().Query(callback); public static IEnumerable Sort(this IEnumerable list, string path, QueryOrderByDirection sortDirection, bool appendSort) diff --git a/PoweredSoft.DynamicLinq/Extensions/QueryableExtensions.cs b/PoweredSoft.DynamicLinq/Extensions/QueryableExtensions.cs index d183d88..18a9336 100644 --- a/PoweredSoft.DynamicLinq/Extensions/QueryableExtensions.cs +++ b/PoweredSoft.DynamicLinq/Extensions/QueryableExtensions.cs @@ -19,15 +19,15 @@ namespace PoweredSoft.DynamicLinq return query; } - public static IQueryable Where(this IQueryable query, Action> callback) + public static IQueryable Where(this IQueryable query, Action callback) => query.Query(callback); - public static IQueryable Query (this IQueryable query, Action> callback) + public static IQueryable Query (this IQueryable query, Action callback) { - var queryBuilder = new WhereBuilder(query); + var queryBuilder = new WhereBuilder(query); callback(queryBuilder); var ret = queryBuilder.Build(); - return ret; + return (IQueryable)ret; } // generic. diff --git a/PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilder.cs b/PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilder.cs index e3b5c06..abb4294 100644 --- a/PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilder.cs +++ b/PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilder.cs @@ -8,16 +8,64 @@ using System.Threading.Tasks; namespace PoweredSoft.DynamicLinq.Fluent { - public class WhereBuilder : WhereBuilderBase + public partial class WhereBuilder { - public IQueryable Query { get; set; } - - public WhereBuilder(IQueryable query) + public IQueryable Query { get; set; } + public Type QueryableType { get; set; } + public List Filters { get; protected set; } = new List(); + + public WhereBuilder(IQueryable query) { Query = query; + QueryableType = query.ElementType; } - public virtual IQueryable Build() + public bool IsNullCheckingEnabled { get; protected set; } = false; + + public virtual WhereBuilder NullChecking(bool check = true) + { + IsNullCheckingEnabled = check; + return this; + } + + public virtual WhereBuilder Compare(string path, ConditionOperators conditionOperators, object value, + QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, + bool and = true, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + { + Filters.Add(new WhereBuilderCondition + { + And = and, + ConditionOperator = conditionOperators, + Path = path, + Value = value, + ConvertStrategy = convertStrategy, + CollectionHandling = collectionHandling, + StringComparisation = stringComparision + }); + + return this; + } + + public virtual WhereBuilder SubQuery(Action subQuery, bool and = true) + { + // create query builder for same type. + var qb = new WhereBuilder(Query); + qb.NullChecking(IsNullCheckingEnabled); + + // callback. + subQuery(qb); + + // create a query part. + var part = new WhereBuilderCondition(); + part.And = and; + part.Conditions = qb.Filters; + Filters.Add(part); + + //return self. + return this; + } + + public virtual IQueryable Build() { // the query. var query = Query; @@ -26,30 +74,31 @@ namespace PoweredSoft.DynamicLinq.Fluent return query; // shared parameter. - var sharedParameter = Expression.Parameter(typeof(T), "t"); + var sharedParameter = Expression.Parameter(QueryableType, "t"); // build the expression. - var filterExpressionMerged = BuildFilterExpression(sharedParameter, Filters); - - // make changes on the query. - query = query.Where(filterExpressionMerged); + var filterExpressionMerged = BuildConditionExpression(sharedParameter, Filters); + + // create the where expression. + var whereExpression = Expression.Call(typeof(Queryable), "Where", new[] { query.ElementType }, query.Expression, filterExpressionMerged); + + // lets see what happens here. + query = query.Provider.CreateQuery(whereExpression); return query; } - protected virtual Expression> BuildFilterExpression(ParameterExpression parameter, List filters) + protected virtual Expression BuildConditionExpression(ParameterExpression parameter, List filters) { - Expression> temp = null; - - + Expression temp = null; filters.ForEach(filter => { - Expression> innerExpression; + Expression innerExpression; if (filter.Conditions?.Any() == true) - innerExpression = BuildFilterExpression(parameter, filter.Conditions); + innerExpression = BuildConditionExpression(parameter, filter.Conditions); else - innerExpression = BuildFilterExpression(parameter, filter); + innerExpression = BuildConditionExpression(parameter, filter); if (temp == null) { @@ -57,10 +106,13 @@ namespace PoweredSoft.DynamicLinq.Fluent } else { + var body = ((LambdaExpression)temp).Body; + var innerEpressionBody = ((LambdaExpression)innerExpression).Body; + if (filter.And) - temp = Expression.Lambda>(Expression.AndAlso(temp.Body, innerExpression.Body), parameter); + temp = Expression.Lambda(Expression.AndAlso(body, innerEpressionBody), parameter); else - temp = Expression.Lambda>(Expression.OrElse(temp.Body, innerExpression.Body), parameter); + temp = Expression.Lambda(Expression.OrElse(body, innerEpressionBody), parameter); } }); @@ -68,9 +120,10 @@ namespace PoweredSoft.DynamicLinq.Fluent return temp; } - protected virtual Expression> BuildFilterExpression(ParameterExpression parameter, WhereBuilderCondition filter) + protected virtual Expression BuildConditionExpression(ParameterExpression parameter, WhereBuilderCondition filter) { - var ret = QueryableHelpers.CreateConditionExpression( + var ret = QueryableHelpers.CreateConditionExpression( + parameter.Type, filter.Path, filter.ConditionOperator, filter.Value, @@ -83,10 +136,5 @@ namespace PoweredSoft.DynamicLinq.Fluent return ret; } - - protected override WhereBuilderBase GetSubQueryBuilder() - { - return new WhereBuilder(Query); - } } } diff --git a/PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilderBase.shortcuts.cs b/PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilder.shortcuts.cs similarity index 53% rename from PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilderBase.shortcuts.cs rename to PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilder.shortcuts.cs index 8a42db5..658f42b 100644 --- a/PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilderBase.shortcuts.cs +++ b/PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilder.shortcuts.cs @@ -4,140 +4,140 @@ using System.Text; namespace PoweredSoft.DynamicLinq.Fluent { - public abstract partial class WhereBuilderBase + public partial class WhereBuilder { - public WhereBuilderBase And(string path, ConditionOperators conditionOperator, object value, + public WhereBuilder And(string path, ConditionOperators conditionOperator, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => Compare(path, conditionOperator, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, and: true, stringComparision: stringComparision); - public WhereBuilderBase Or(string path, ConditionOperators conditionOperator, object value, + public WhereBuilder Or(string path, ConditionOperators conditionOperator, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => Compare(path, conditionOperator, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, and: false, stringComparision: stringComparision); - public WhereBuilderBase And(Action subQuery) + public WhereBuilder And(Action subQuery) => SubQuery(subQuery, true); - public WhereBuilderBase Or(Action subQuery) + public WhereBuilder Or(Action subQuery) => SubQuery(subQuery, false); #region equal - public WhereBuilderBase Equal(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder Equal(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => And(path, ConditionOperators.Equal, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public WhereBuilderBase AndEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder AndEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => And(path, ConditionOperators.Equal, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public WhereBuilderBase OrEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder OrEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => Or(path, ConditionOperators.Equal, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); #endregion #region not equal - public WhereBuilderBase NotEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder NotEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => And(path, ConditionOperators.NotEqual, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public WhereBuilderBase AndNotEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder AndNotEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => And(path, ConditionOperators.NotEqual, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public WhereBuilderBase OrNotEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder OrNotEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => Or(path, ConditionOperators.NotEqual, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); #endregion #region GreaterThan - public WhereBuilderBase GreaterThan(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) + public WhereBuilder GreaterThan(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) => And(path, ConditionOperators.GreaterThan, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling); - public WhereBuilderBase AndGreaterThan(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) + public WhereBuilder AndGreaterThan(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) => And(path, ConditionOperators.GreaterThan, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling); - public WhereBuilderBase OrGreaterThan(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) + public WhereBuilder OrGreaterThan(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) => Or(path, ConditionOperators.GreaterThan, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling); #endregion #region GreaterThanOrEqual - public WhereBuilderBase GreaterThanOrEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) + public WhereBuilder GreaterThanOrEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) => And(path, ConditionOperators.GreaterThanOrEqual, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling); - public WhereBuilderBase AndGreaterThanOrEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) + public WhereBuilder AndGreaterThanOrEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) => And(path, ConditionOperators.GreaterThanOrEqual, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling); - public WhereBuilderBase OrGreaterThanOrEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) + public WhereBuilder OrGreaterThanOrEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) => Or(path, ConditionOperators.GreaterThanOrEqual, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling); #endregion #region LessThan - public WhereBuilderBase LessThan(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) + public WhereBuilder LessThan(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) => And(path, ConditionOperators.LessThan, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling); - public WhereBuilderBase AndLessThan(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) + public WhereBuilder AndLessThan(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) => And(path, ConditionOperators.LessThan, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling); - public WhereBuilderBase OrLessThan(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) + public WhereBuilder OrLessThan(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) => Or(path, ConditionOperators.LessThan, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling); #endregion #region LessThanOrEqual - public WhereBuilderBase LessThanOrEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) + public WhereBuilder LessThanOrEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) => And(path, ConditionOperators.LessThanOrEqual, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling); - public WhereBuilderBase AndLessThanOrEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) + public WhereBuilder AndLessThanOrEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) => And(path, ConditionOperators.LessThanOrEqual, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling); - public WhereBuilderBase OrLessThanOrEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) + public WhereBuilder OrLessThanOrEqual(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any) => Or(path, ConditionOperators.LessThanOrEqual, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling); #endregion #region contains - public WhereBuilderBase Contains(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder Contains(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => And(path, ConditionOperators.Contains, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public WhereBuilderBase AndContains(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder AndContains(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => And(path, ConditionOperators.Contains, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public WhereBuilderBase OrContains(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder OrContains(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => Or(path, ConditionOperators.Contains, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); #endregion #region starts with - public WhereBuilderBase StartsWith(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder StartsWith(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => And(path, ConditionOperators.StartsWith, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public WhereBuilderBase AndStartsWith(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder AndStartsWith(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => And(path, ConditionOperators.StartsWith, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public WhereBuilderBase OrStartsWith(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder OrStartsWith(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => Or(path, ConditionOperators.StartsWith, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); #endregion #region ends with - public WhereBuilderBase EndsWith(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder EndsWith(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => And(path, ConditionOperators.EndsWith, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public WhereBuilderBase AndEndsWith(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder AndEndsWith(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => And(path, ConditionOperators.EndsWith, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public WhereBuilderBase OrEndsWith(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder OrEndsWith(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => Or(path, ConditionOperators.EndsWith, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); #endregion #region In - public WhereBuilderBase In(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder In(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => And(path, ConditionOperators.In, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public WhereBuilderBase AndIn(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder AndIn(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => And(path, ConditionOperators.In, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public WhereBuilderBase OrIn(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder OrIn(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => Or(path, ConditionOperators.In, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); #endregion #region NotIn - public WhereBuilderBase NotIn(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder NotIn(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => And(path, ConditionOperators.NotIn, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public WhereBuilderBase AndNotIn(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder AndNotIn(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => And(path, ConditionOperators.NotIn, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); - public WhereBuilderBase OrNotIn(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) + public WhereBuilder OrNotIn(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) => Or(path, ConditionOperators.NotIn, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); #endregion } diff --git a/PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilderBase.cs b/PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilderBase.cs deleted file mode 100644 index c6dc93f..0000000 --- a/PoweredSoft.DynamicLinq/Fluent/WhereBuilder/WhereBuilderBase.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.CodeDom; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PoweredSoft.DynamicLinq.Fluent -{ - public abstract partial class WhereBuilderBase - { - public bool IsNullCheckingEnabled { get; protected set; } = false; - - public List Filters { get; protected set; } = new List(); - - public virtual WhereBuilderBase NullChecking(bool check = true) - { - IsNullCheckingEnabled = check; - return this; - } - - public virtual WhereBuilderBase Compare(string path, ConditionOperators conditionOperators, object value, - QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, - bool and = true, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) - { - Filters.Add(new WhereBuilderCondition - { - And = and, - ConditionOperator = conditionOperators, - Path = path, - Value = value, - ConvertStrategy = convertStrategy, - CollectionHandling = collectionHandling, - StringComparisation = stringComparision - }); - - return this; - } - - protected abstract WhereBuilderBase GetSubQueryBuilder(); - - public virtual WhereBuilderBase SubQuery(Action subQuery, bool and = true) - { - // create query builder for same type. - var qb = GetSubQueryBuilder(); - qb.NullChecking(IsNullCheckingEnabled); - - // callback. - subQuery(qb); - - // create a query part. - var part = new WhereBuilderCondition(); - part.And = and; - part.Conditions = qb.Filters; - Filters.Add(part); - - //return self. - return this; - } - } -} diff --git a/PoweredSoft.DynamicLinq/Helpers/QueryableHelpers.cs b/PoweredSoft.DynamicLinq/Helpers/QueryableHelpers.cs index 7a0c772..128aa15 100644 --- a/PoweredSoft.DynamicLinq/Helpers/QueryableHelpers.cs +++ b/PoweredSoft.DynamicLinq/Helpers/QueryableHelpers.cs @@ -79,6 +79,8 @@ namespace PoweredSoft.DynamicLinq.Helpers return ret; } + + public static IQueryable GroupBy(IQueryable query, Type type, List<(string path, string propertyName)> parts, Type groupToType = null, Type equalityCompareType = null) { // EXPRESSION @@ -270,16 +272,16 @@ namespace PoweredSoft.DynamicLinq.Helpers throw new NotSupportedException($"{convertStrategy} supplied is not recognized"); } - public static IQueryable CreateOrderByExpression(IQueryable query, string sortPath, QueryOrderByDirection sortDirection, bool appendSort = true) + public static IQueryable CreateOrderByExpression(IQueryable query, string path, QueryOrderByDirection direction, bool append = true) { var parameter = Expression.Parameter(query.ElementType, "t"); - var member = QueryableHelpers.ResolvePathForExpression(parameter, sortPath); + var member = QueryableHelpers.ResolvePathForExpression(parameter, path); string sortCommand = null; - if (sortDirection == QueryOrderByDirection.Descending) - sortCommand = appendSort == false ? "OrderByDescending" : "ThenByDescending"; + if (direction == QueryOrderByDirection.Descending) + sortCommand = append == false ? "OrderByDescending" : "ThenByDescending"; else - sortCommand = appendSort == false ? "OrderBy" : "ThenBy"; + sortCommand = append == false ? "OrderBy" : "ThenBy"; var expression = Expression.Lambda(member, parameter); @@ -420,9 +422,27 @@ namespace PoweredSoft.DynamicLinq.Helpers throw new NotSupportedException($"{collectionHandling} is not supported"); } - - public static Expression> CreateConditionExpression(string path, + public static Expression> CreateConditionExpression( + string path, + ConditionOperators condition, + object value, + QueryConvertStrategy convertStrategy, + QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, + ParameterExpression parameter = null, + bool nullChecking = false, + StringComparison? stringComparision = null) + { + var ret = CreateConditionExpression(typeof(T), path, condition, value, convertStrategy, + collectionHandling: collectionHandling, + parameter: parameter, + nullChecking: nullChecking, + stringComparision: stringComparision) as Expression>; + return ret; + } + + public static Expression CreateConditionExpression(Type type, + string path, ConditionOperators condition, object value, QueryConvertStrategy convertStrategy, @@ -432,12 +452,11 @@ namespace PoweredSoft.DynamicLinq.Helpers StringComparison? stringComparision = null) { if (parameter == null) - parameter = Expression.Parameter(typeof(T), "t"); + parameter = Expression.Parameter(type, "t"); var parts = path.Split('.').ToList(); - var result = InternalCreateConditionExpression(1, typeof(T), parameter, parameter, parts, condition, value, convertStrategy, collectionHandling, nullChecking, stringComparision); - var ret = result as Expression>; - return ret; + var result = InternalCreateConditionExpression(1, type, parameter, parameter, parts, condition, value, convertStrategy, collectionHandling, nullChecking, stringComparision); + return result; } public static bool IsEnumerable(MemberExpression member)