Created
January 10, 2024 14:08
-
-
Save vlapenkov/1d8949e531df794d7e38a12081a11f13 to your computer and use it in GitHub Desktop.
Expressions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Expression<Func<Db.Model.Car, bool>> CreateNestedExpressions(IDictionary<string, object> filters) | |
{ | |
var param = Expression.Parameter(typeof(Db.Model.Car), "p"); | |
Expression? body = null; | |
foreach (var filter in filters) | |
{ | |
Expression member = param; | |
foreach (var namePart in filter.Key.Split('.')) | |
{ | |
member = Expression.Property(member, namePart); | |
} | |
var constant = Expression.Constant(filter.Value); | |
var expression = Expression.Equal(member, constant); | |
body = body == null ? expression : Expression.AndAlso(body, expression); | |
} | |
return Expression.Lambda<Func<Db.Model.Car, bool>>(body, param); | |
} | |
//ТОЖЕ самое только без точек | |
public static Expression<Func<Db.Model.Car, bool>> CreateEqualExpression(IDictionary<string, Guid> filters) | |
{ | |
var param = Expression.Parameter(typeof(Db.Model.Car), "p"); | |
Expression? body = null; | |
foreach (var pair in filters) | |
{ | |
var member = Expression.Property(param, pair.Key); | |
var constant = Expression.Constant(pair.Value); | |
var expression = Expression.Equal(member, constant); | |
body = body == null ? expression : Expression.AndAlso(body, expression); | |
} | |
return Expression.Lambda<Func<Db.Model.Car, bool>>(body, param); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Expression<Func<TEntity, bool>> CreateExpression<TEntity>(LambdaExpression baseLambda, Expression<Func<TEntity, bool>> filter) where TEntity : class | |
{ | |
var param = baseLambda.Parameters[0]; | |
var left = baseLambda.Body; | |
var right = filter.Body.ReplaceParameter(filter.Parameters[0], param); | |
var result = Expression.AndAlso(left, right); | |
return Expression.Lambda<Func<TEntity, bool>>(result, param); | |
} | |
public static class ExpressionExtensions | |
{ | |
public static Expression ReplaceParameter(this Expression expression, ParameterExpression source, Expression target) | |
{ | |
return new ParameterReplacer { Source = source, Target = target }.Visit(expression); | |
} | |
class ParameterReplacer : ExpressionVisitor | |
{ | |
public ParameterExpression Source; | |
public Expression Target; | |
protected override Expression VisitParameter(ParameterExpression node) | |
{ | |
return node == Source ? Target : base.VisitParameter(node); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment