gpt4 book ai didi

c# - 在 C# 中组合 BinaryExpression 和 Expression>

转载 作者:行者123 更新时间:2023-12-03 02:15:28 38 4
gpt4 key购买 nike

如何组合 BinaryExpressionExpression<Func<dynamic / T, bool>>

例如:

void AddGlobalFilter<T>(Expression<Func<T, bool>> expr)
{
var parameter = Expression.Parameter(type, "t");
var member = Expression.Property(filter.Parameter, field);
var constant = Expression.Constant(null);
var body = Expression.Equal(member, constant);

var combine = Expression.AndAlso(body, expr);
}

我正在尝试定义global filter适用于 Entity Framework (EF) Core。问题是我必须manually combine multiple filters .

可以在ModelBuilder中添加一个过滤器如果模型实现 IDbDeleted接口(interface)。
可以针对特定型号手动添加另一个。基本想法是我有一个所有表达式的列表,然后将它们组合起来:

var expression = listExpressions.First();
foreach (var second in listExpressions.Skip(1))
{
expression = Expression.AndAlso(expression, second);
}
var lambdaExpression = Expression.Lambda(expression, parameter);
modelBuilder.Entity(item.Key).HasQueryFilter(lambdaExpression);

当然我得到了错误(第一个来自 Expression.Equal ,第二个来自 t => t... ):

The filter expression 't => t => (Not(t. ...

编辑:代码看起来像这样:

[Table("MyEntities")]
public class DbMyEntity : IDeleted
{
public string Name { get; set; }
public DateTime? DateTimeDeleted { get; set; }
}

public interface IDeleted
{
DateTime? DateTimeDeleted { get; set; }
}

public class MyContext : IdentityDbContext
{
private Dictionary<Type, List<Expression>> dict = new Dictionary<Type, List<Expression>>();
private Dictionary<Type, ParameterExpression> dictParameter = new Dictionary<Type, ParameterExpression>();

private ParameterExpression GetParameter(Type type)
{
if (!this.dictParameter.ContainsKey(type))
{
this.dictParameter.Add(type, Expression.Parameter(type, "t"));
}
return this.dictParameter[type];
}

private void AddToDict(Type type, Expression expr)
{
if (!this.dict.ContainsKey(type))
{
this.dict.Add(type, new List<Expression>());
this.GetParameter(type); //Just to create ParameterExpression if not exists.
}

this.dict[type].Add(expr);
}

private void AddToDict<T>(Expression<Func<T, bool>> expr)
{
this.AddToDict(typeof(T), expr);
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{

base.OnModelCreating(modelBuilder);

foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
if (typeof(IDeleted).IsAssignableFrom(entity.ClrType))
{
var member = Expression.Property(this.GetParameter(entity.ClrType), "DateTimeDeleted");
var constant = Expression.Constant(null);
var body = Expression.Equal(member, constant);
this.AddToDict(entity.ClrType, body);
}
}

//This is done in another project in same solution. See comment bellow.
this.AddToDict<DbMyEntity>(t => t.Name == null || t.Name == "Something");
//foreach (var builderType in allDllModules)
//{
// if (builderType != null && builderType != typeof(ICustomModelBuilder))
// {
// var builder = (ICustomModelBuilder)Activator.CreateInstance(builderType);
// builder.Build(modelBuilder);
// }
//}

foreach (var item in this.dict)
{
var expression = item.Value.First();
foreach (var second in item.Value.Skip(1))
{
expression = Expression.AndAlso(expression, second);
}
var lambdaExpression = Expression.Lambda(expression, this.dictParameter[item.Key]);
modelBuilder.Entity(item.Key).HasQueryFilter(lambdaExpression);
}
}
}

最佳答案

您正在混合expressionslambda expressions 。有很多帖子展示了如何组合 lambda 表达式,但最重要的部分是从 lambda 表达式 bodies 组合表达式。并重新绑定(bind)parameters .

后者通常是通过自定义ExpressionVisitor来实现的像这样:

using System.Linq.Expressions;

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);
}
}
}

现在关于 EF Core 组合查询过滤器。

对于您正在做的事情来说,使用字典和表达式列表似乎过于复杂。自 IMutableEntityType提供对 QueryFilter 的读/写访问,同样可以通过一小组自定义扩展方法来实现。

它们都进入这样的类中:

using System;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

public static class QueryFilterExtensions
{
}

第一种方法:

public static void AddQueryFilter(this IMutableEntityType target, LambdaExpression filter)
{
if (target.QueryFilter == null)
target.QueryFilter = filter;
else
{
var parameter = target.QueryFilter.Parameters[0];
var left = target.QueryFilter.Body;
var right = filter.Body.ReplaceParameter(filter.Parameters[0], parameter);
var body = Expression.AndAlso(left, right);
target.QueryFilter = Expression.Lambda(body, parameter);
}
}

这是一种非泛型方法,它使用 AndAlso (C# &&) 运算符将现有过滤器与传递的过滤器组合起来,并显示了前面提到的 lambda 表达式组合原理。

但是它并不是那么直接有用,就像在实体类型配置循环中一样(它可以,但需要您手动构建 lambda 表达式,而不是让 C# 编译器这样做)。那么这里就来了第二种方法:

public static void AddQueryFilter<T>(this IMutableEntityType target, Expression<Func<T, bool>> filter)
{
LambdaExpression targetFilter = filter;
if (target.ClrType != typeof(T))
{
var parameter = Expression.Parameter(target.ClrType, "e");
var body = filter.Body.ReplaceParameter(filter.Parameters[0], parameter);
targetFilter = Expression.Lambda(body, parameter);
}
target.AddQueryFilter(targetFilter);
}

这是一个泛型方法 - 不太类型安全,但允许您使用编译时 lambda 表达式并将其绑定(bind)到实际实体类型,如下所示:

foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
if (typeof(IDeleted).IsAssignableFrom(entityType.ClrType))
entityType.AddQueryFilter<IDeleted>(e => e.DateTimeDeleted == null);
}

看起来更好,不是吗:)

最后一个自定义扩展方法是对标准 EF Core 通用 HasQueryFilter 的补充(替换)方法:

public static EntityTypeBuilder<TEntity> AddQueryFilter<TEntity>(this EntityTypeBuilder<TEntity> target, Expression<Func<TEntity, bool>> filter)
where TEntity : class
{
target.Metadata.AddQueryFilter(filter);
return target;
}

并允许您替换

this.AddToDict<DbMyEntity>(t => t.Name == null || t.Name == "Something");

用起来更方便

modelBuilder.Entity<DbMyEntity>()
.AddQueryFilter(t => t.Name == null || t.Name == "Something");

更新 (EF Core 3.0): QueryFilter 属性已替换为 GetQueryFilterSetQueryFilter 扩展方法。

关于c# - 在 C# 中组合 BinaryExpression 和 Expression<Func<dynamic, bool>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51497089/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com