diff --git a/src/CSESoftware.Repository/Builder/ExpressionExtensions.cs b/src/CSESoftware.Repository/Builder/ExpressionExtensions.cs new file mode 100644 index 0000000..3f86096 --- /dev/null +++ b/src/CSESoftware.Repository/Builder/ExpressionExtensions.cs @@ -0,0 +1,32 @@ +using System; +using System.Linq; +using System.Linq.Expressions; + +namespace CSESoftware.Repository.Builder +{ + internal static class ExpressionExtensions + { + internal static Expression> AndAlso(this Expression> left, Expression> right) + { + var parameter = Expression.Parameter(typeof(T), "entity"); + + var leftVisitor = new ReplaceExpressionVisitor(left.Parameters.First(), parameter); + var leftBody = leftVisitor.Visit(left.Body); + + var rightVisitor = new ReplaceExpressionVisitor(right.Parameters.First(), parameter); + var rightBody = rightVisitor.Visit(right.Body); + + var body = Expression.AndAlso(leftBody!, rightBody!); + + return Expression.Lambda>(body, parameter); + } + + private class ReplaceExpressionVisitor(Expression oldValue, Expression newValue) : ExpressionVisitor + { + public override Expression Visit(Expression expression) + { + return expression == oldValue ? newValue : base.Visit(expression); + } + } + } +} diff --git a/src/CSESoftware.Repository/Builder/QueryBuilder.cs b/src/CSESoftware.Repository/Builder/QueryBuilder.cs index 693239b..55f0850 100644 --- a/src/CSESoftware.Repository/Builder/QueryBuilder.cs +++ b/src/CSESoftware.Repository/Builder/QueryBuilder.cs @@ -12,7 +12,6 @@ public class QueryBuilder where TEntity : class, IEntity protected internal readonly IQuery Entity; internal List> Ordering; - public QueryBuilder() { Entity = new Query @@ -35,6 +34,12 @@ public QueryBuilder Where(Expression> expression) return this; } + public QueryBuilder WhereAlso(Expression> expression) + { + Entity.Predicate = Entity.Predicate.AndAlso(expression); + return this; + } + public QueryBuilder OrderBy(Func, IOrderedQueryable> order) { Entity.OrderBy = order; diff --git a/src/CSESoftware.Repository/CSESoftware.Repository.csproj b/src/CSESoftware.Repository/CSESoftware.Repository.csproj index 45e1699..e93d1f0 100644 --- a/src/CSESoftware.Repository/CSESoftware.Repository.csproj +++ b/src/CSESoftware.Repository/CSESoftware.Repository.csproj @@ -6,8 +6,8 @@ CSESoftware.Repository true CSE Software, Inc. - 2023 CSE Software, Inc. - 2.4.0 + 2024 CSE Software, Inc. + 2.5.0 2.0.0.0 A generic repository pattern. packageIcon.png @@ -17,6 +17,7 @@ git Apache-2.0 true + latest