gpt4 book ai didi

c# - 如何修改Expression>的类型参数?

转载 作者:太空狗 更新时间:2023-10-29 21:09:15 24 4
gpt4 key购买 nike

我有以下实例:

Expression<Func<IRequiredDate, bool>>

我希望将其转换为以下实例,以便它可用于在 Entity Framework 中运行查询:

Expression<Func<TModel, bool>>

这将允许我对任何实现 IRequiredDate 的模型使用通用过滤查询,例如:

// In some repository function:
var query = DbContext.Set<Order>()
.FilterByDateRange(DateTime.Today, DateTime.Today);

var query = DbContext.Set<Note>()
.FilterByDateRange(DateTime.Today, DateTime.Today);

var query = DbContext.Set<Complaint>()
.FilterByDateRange(DateTime.Today, DateTime.Today);


// The general purpose function, can filter for any model implementing IRequiredDate
public static IQueryable<TModel> FilterByDate<TModel>(IQueryable<TModel> query, DateTime startDate, DateTime endDate) where TModel : IRequiredDate
{
// This will NOT WORK, as E/F won't accept an expression of type IRequiredDate, even though TModel implements IRequiredDate
// Expression<Func<IRequiredDate, bool>> dateRangeFilter = x => x.Date >= startDate && x.Date <= endDate;
// query = query.Where(dateRangeFilter);

// This also WON'T WORK, x.Date is compiled into the expression as a member of IRequiredDate instead of TModel, so E/F knocks it back for the same reason:
// Expression<Func<TModel, bool>> dateRangeFilter = x => x.Date >= startDate && x.Date <= endDate;
// query = query.Where(dateRangeFilter);

// All you need is lov.... uh... something like this:
Expression<Func<IRequiredDate, bool>> dateRangeFilter = x => x.Date >= startDate && x.Date <= endDate;
Expression<Func<TModel, bool>> dateRangeFilterForType = ConvertExpressionType<IRequiredDate, TModel>(dateRangeFilter); // Must convert the expression from one type to another
query = query.Where(dateRangeFilterForType) // Ahhhh. this will work.

return query;
}

public static ConvertExpressionType<TInterface, TModel>(Expression<Func<TInterface, bool>> expression)
where TModel : TInterface // It must implement the interface, since we're about to translate them
{
Expression<Func<TModel, bool>> newExpression = null;

// TODO: How to convert the contents of expression into newExpression, modifying the
// generic type parameter along the way??

return newExpression;
}

我知道它们是不同的类型,不能转换。但是我想知道是否有办法创建一个新的 Expression<Func<TModel, bool>> , 然后根据 Expression<Func<IRequiredDate, bool>> 的内容重建它提供,从 IRequiredDate 切换任何类型引用至 TModel在此过程中。

这可以做到吗?

最佳答案

所以实际进行映射的方法并不那么难,但遗憾的是,我没有看到一个通用化它的好方法。这是一个采用 Func<T1, TResult> 的方法并将其映射到一个委托(delegate),其中参数比 T1 更派生。 :

public static Expression<Func<NewParam, TResult>> Foo<NewParam, OldParam, TResult>(
Expression<Func<OldParam, TResult>> expression)
where NewParam : OldParam
{
var param = Expression.Parameter(typeof(NewParam));
return Expression.Lambda<Func<NewParam, TResult>>(
expression.Body.Replace(expression.Parameters[0], param)
, param);
}

这使用了 Replace用另一个表达式替换一个表达式的所有实例的方法。定义是:

internal class ReplaceVisitor : ExpressionVisitor
{
private readonly Expression from, to;
public ReplaceVisitor(Expression from, Expression to)
{
this.from = from;
this.to = to;
}
public override Expression Visit(Expression node)
{
return node == from ? to : base.Visit(node);
}
}

public static Expression Replace(this Expression expression,
Expression searchEx, Expression replaceEx)
{
return new ReplaceVisitor(searchEx, replaceEx).Visit(expression);
}

现在我们可以像这样使用这个方法(应该给它一个更好的名字):

Expression<Func<object, bool>> oldExpression = whatever;
Expression<Func<string, bool>> newExpression =
Foo<string, object, bool>(oldExpression);

当然还有自 Func就其参数而言实际上是协变的,我们可以确定对此方法的任何调用都会生成不会添加运行时故障点的表达式。

你可以简单地为 Func<T1, T2, TResult> 制作这个版本,依此类推直到 16 种不同类型的 Func如果你愿意,只需为每个参数创建一个参数表达式,然后用新的替换所有旧的。这会很乏味,但只需遵循模式即可。考虑到旧参数类型和新参数类型都需要有一个通用参数,并且没有办法推断参数,那会变得......困惑。

关于c# - 如何修改Expression<Func<???, bool>>的类型参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21154906/

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