forked from ddrilling/AsbCloudServer
Add PredicateBuilder to build complex predicate expressions
This commit is contained in:
parent
759d6125b5
commit
e548425b18
80
AsbCloudInfrastructure/PredicateBuilder.cs
Normal file
80
AsbCloudInfrastructure/PredicateBuilder.cs
Normal 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
|
@ -11,6 +11,7 @@ using Org.BouncyCastle.Asn1.Ocsp;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -80,27 +81,28 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
private IQueryable<ProcessMap> BuildQuery(IEnumerable<ProcessMapRequest> requests)
|
private IQueryable<ProcessMap> BuildQuery(IEnumerable<ProcessMapRequest> requests)
|
||||||
{
|
{
|
||||||
var query = GetQuery();
|
var query = GetQuery();
|
||||||
Func<ProcessMap, bool>? p = null;
|
if (requests.Any())
|
||||||
foreach (var request in requests)
|
|
||||||
{
|
{
|
||||||
var p2 = (ProcessMap map) => map.IdWell == request.IdWell;
|
Expression<Func<ProcessMap, bool>> predicate = map => false;
|
||||||
|
foreach (var request in requests)
|
||||||
if (request.IdWellSectionType is not null)
|
|
||||||
p2 = (ProcessMap map) => p2(map) && map.IdWellSectionType == request.IdWellSectionType;
|
|
||||||
|
|
||||||
if (request.UpdateFrom is not null)
|
|
||||||
{
|
{
|
||||||
var timezone = wellService.GetTimezone(request.IdWell);
|
Expression<Func<ProcessMap, bool>> predicate2 = map => map.IdWell == request.IdWell;
|
||||||
var updateFromUtc = request.UpdateFrom?.ToUtcDateTimeOffset(timezone.Hours);
|
|
||||||
p2 = (ProcessMap map) => p2(map) && map.LastUpdate >= updateFromUtc;
|
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);
|
||||||
}
|
}
|
||||||
|
query = query.Where(predicate);
|
||||||
p = p is null
|
|
||||||
? p2
|
|
||||||
: (ProcessMap map) => p(map) || p2(map);
|
|
||||||
}
|
}
|
||||||
if(p is not null)
|
|
||||||
query.Where(p);
|
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
protected override ProcessMapDto Convert(ProcessMap entity)
|
protected override ProcessMapDto Convert(ProcessMap entity)
|
||||||
|
Loading…
Reference in New Issue
Block a user