如何修改Expression <Func <???,bool >>的类型参数?

Bre*_*ill 7 c# linq expression entity-framework

我有以下实例:

Expression<Func<IRequiredDate, bool>>
Run Code Online (Sandbox Code Playgroud)

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

Expression<Func<TModel, bool>>
Run Code Online (Sandbox Code Playgroud)

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

// 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;
}
Run Code Online (Sandbox Code Playgroud)

我知道他们是不同的类型,不能演员.不过,我想知道是否有是创建一个新的方式Expression<Func<TModel, bool>>,然后重建它的基础上的内容Expression<Func<IRequiredDate, bool>>提供,从交换任何类型的引用IRequiredDate,以TModel在这个过程中.

可以这样做吗?

Ser*_*rvy 10

所以实际进行映射的方法并不那么难,但遗憾的是,我没有一种很好的方法可以看出它的推广.这是一个方法,它将a Func<T1, TResult>和map映射到一个委托,其中参数是派生的,而不是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);
}
Run Code Online (Sandbox Code Playgroud)

这使用该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);
}
Run Code Online (Sandbox Code Playgroud)

现在我们可以使用这个方法(应该给出一个更好的名字),如下所示:

Expression<Func<object, bool>> oldExpression = whatever;
Expression<Func<string, bool>> newExpression =
    Foo<string, object, bool>(oldExpression);
Run Code Online (Sandbox Code Playgroud)

当然,因为Func它实际上是关于其参数的协变,所以我们可以确定对此方法的任何调用都会生成不会添加运行时故障点的表达式.

如果你愿意Func<T1, T2, TResult>Func话,你可以通过16种不同的类型轻松制作这个版本,等等,只需为每个类型创建一个参数表达式,并用新的替换所有旧的参数表达式.这很乏味,但只是遵循这种模式.鉴于旧的和新的参数类型都需要有一个泛型参数,并且没有办法推断出参数,那就太麻烦了.