Skip to content

Commit

Permalink
Add WhereAlso (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
TieDyeGeek authored Jun 5, 2024
1 parent 8e3b65e commit ec06ee7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
32 changes: 32 additions & 0 deletions src/CSESoftware.Repository/Builder/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Linq;
using System.Linq.Expressions;

namespace CSESoftware.Repository.Builder
{
internal static class ExpressionExtensions
{
internal static Expression<Func<T, bool>> AndAlso<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> 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<Func<T, bool>>(body, parameter);
}

private class ReplaceExpressionVisitor(Expression oldValue, Expression newValue) : ExpressionVisitor
{
public override Expression Visit(Expression expression)
{
return expression == oldValue ? newValue : base.Visit(expression);
}
}
}
}
7 changes: 6 additions & 1 deletion src/CSESoftware.Repository/Builder/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class QueryBuilder<TEntity> where TEntity : class, IEntity
protected internal readonly IQuery<TEntity> Entity;
internal List<Ordering<TEntity>> Ordering;


public QueryBuilder()
{
Entity = new Query<TEntity>
Expand All @@ -35,6 +34,12 @@ public QueryBuilder<TEntity> Where(Expression<Func<TEntity, bool>> expression)
return this;
}

public QueryBuilder<TEntity> WhereAlso(Expression<Func<TEntity, bool>> expression)
{
Entity.Predicate = Entity.Predicate.AndAlso(expression);
return this;
}

public QueryBuilder<TEntity> OrderBy(Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> order)
{
Entity.OrderBy = order;
Expand Down
5 changes: 3 additions & 2 deletions src/CSESoftware.Repository/CSESoftware.Repository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<AssemblyName>CSESoftware.Repository</AssemblyName>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>CSE Software, Inc.</Authors>
<Copyright>2023 CSE Software, Inc.</Copyright>
<Version>2.4.0</Version>
<Copyright>2024 CSE Software, Inc.</Copyright>
<Version>2.5.0</Version>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<Description>A generic repository pattern.</Description>
<PackageIcon>packageIcon.png</PackageIcon>
Expand All @@ -17,6 +17,7 @@
<RepositoryType>git</RepositoryType>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit ec06ee7

Please sign in to comment.