Add PredicateBuilder to build complex predicate expressions

This commit is contained in:
ngfrolov 2023-02-03 16:00:45 +05:00
parent 759d6125b5
commit e548425b18
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7
2 changed files with 99 additions and 17 deletions

View File

@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace AsbCloudInfrastructure
{
#nullable enable
/// <summary>
/// stolen from https://github.com/lotosbin/BinbinPredicateBuilder
/// </summary>
public static class PredicateBuilder
{
/// <summary>
/// Combines the first predicate with the second using the logical "and".
/// </summary>
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.AndAlso);
}
/// <summary>
/// Combines the first predicate with the second using the logical "or".
/// </summary>
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.OrElse);
}
/// <summary>
/// Negates the predicate.
/// </summary>
public static Expression<Func<T, bool>> Not<T>(this Expression<Func<T, bool>> expression)
{
var negated = Expression.Not(expression.Body);
return Expression.Lambda<Func<T, bool>>(negated, expression.Parameters);
}
private static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
{
var map = first.Parameters
.Select((f, i) => new { f, s = second.Parameters[i] })
.ToDictionary(p => p.s, p => p.f);
var tryReplaceParametr = (Expression node) =>
{
if (node is ParameterExpression parameter)
if (map.TryGetValue(parameter, out ParameterExpression? replacement))
return replacement;
return node;
};
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
}
class ParameterRebinder : ExpressionVisitor
{
private readonly Dictionary<ParameterExpression, ParameterExpression> map;
private ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
{
this.map = map;
}
public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
{
return new ParameterRebinder(map).Visit(exp);
}
protected override Expression VisitParameter(ParameterExpression parametr)
{
if (map.TryGetValue(parametr, out ParameterExpression? replacement))
parametr = replacement;
return parametr;
}
}
}
}
#nullable disable

View File

@ -11,6 +11,7 @@ using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
@ -80,27 +81,28 @@ namespace AsbCloudInfrastructure.Repository
private IQueryable<ProcessMap> BuildQuery(IEnumerable<ProcessMapRequest> requests)
{
var query = GetQuery();
Func<ProcessMap, bool>? p = null;
foreach (var request in requests)
if (requests.Any())
{
var p2 = (ProcessMap map) => map.IdWell == request.IdWell;
if (request.IdWellSectionType is not null)
p2 = (ProcessMap map) => p2(map) && map.IdWellSectionType == request.IdWellSectionType;
if (request.UpdateFrom is not null)
Expression<Func<ProcessMap, bool>> predicate = map => false;
foreach (var request in requests)
{
var timezone = wellService.GetTimezone(request.IdWell);
var updateFromUtc = request.UpdateFrom?.ToUtcDateTimeOffset(timezone.Hours);
p2 = (ProcessMap map) => p2(map) && map.LastUpdate >= updateFromUtc;
Expression<Func<ProcessMap, bool>> predicate2 = map => map.IdWell == request.IdWell;
if (request.IdWellSectionType is not null)
predicate2 = predicate2.And(map => map.IdWellSectionType == request.IdWellSectionType);
if (request.UpdateFrom is not null)
{
var timezone = wellService.GetTimezone(request.IdWell);
var updateFromUtc = request.UpdateFrom?.ToUtcDateTimeOffset(timezone.Hours);
predicate2 = predicate2.And(map => map.LastUpdate >= updateFromUtc);
}
predicate = predicate.Or(predicate2);
}
p = p is null
? p2
: (ProcessMap map) => p(map) || p2(map);
query = query.Where(predicate);
}
if(p is not null)
query.Where(p);
return query;
}
protected override ProcessMapDto Convert(ProcessMap entity)