Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WhereAlso to QueryBuilder #13

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading