加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:17:59 所属栏目:百科 来源:网络整理
导读:我有以下实例: ExpressionFuncIRequiredDate,bool 我希望将其转换为以下实例,因此它可用于在Entity Framework中运行查询: ExpressionFuncTModel,bool 这将允许我对任何实现IRequiredDate的Model使用通用过滤查询,例如: // In some repository function:va
我有以下实例:
Expression<Func<IRequiredDate,bool>>

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

Expression<Func<TModel,bool>>

这将允许我对任何实现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;
}

我知道他们是不同的类型,不能演员.但是我想知道是否有办法创建一个新的表达式< Func< TModel,bool>>,然后根据Expression< Func< IRequiredDate,bool>>的内容重建它.提供,在过程中将任何类型的引用从IRequiredDate切换到TModel.

可以这样做吗?

解决方法

所以实际进行映射的方法并不那么难,但遗憾的是,我没有一种很好的方法可以看出它的推广.这是一个采用Func< T1,TResult>的方法.并将其映射到一个委托,其中参数是比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,如果您愿意,只需为每个类型创建一个参数表达式,并替换所有旧的新的.这很乏味,但只是遵循这种模式.鉴于旧的和新的参数类型都需要有一个泛型参数,并且没有办法推断出参数,那就太麻烦了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读