存储表达式< Func< T,bool>.在哪里作为阶级财产 [英] store Expression<Func<T, bool>> where as a class property

查看:71
本文介绍了存储表达式< Func< T,bool>.在哪里作为阶级财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展System.Web.UI.WebControls.GridView控件的类.我想拥有一个可以保存我的EF表达式的属性,以便在整个控件中使用.

I have a class that extends the System.Web.UI.WebControls.GridView control. I want to have a property that can save my EF expression to use throughout the control.

问题是,T没有定义.

public sealed class NCGridView : GridView
{
    private Expression<Func<T, bool>> _where;

    public void LoadWhere(Expression<Func<T, bool>> where)
    {
        _where = where;
    }
}

RedHat建议尝试

RedHat suggestion attempt

    private Expression<Func<BaseModel, bool>> _where;

    public void LoadWhere<T>(Expression<Func<T, bool>> where) where T : BaseModel
    {
       // Cannot cast from: Expression<Func<T, bool>> to:  Expression<Func<BaseModel, bool>>
        _where = where;
    }

推荐答案

答案2:更新:

public Expression<Func<BaseModel, bool>> LoadWhere<T>(Expression<Func<T, bool>> where) where T : BaseModel
{
    where = LambdaExpression.Lambda<Func<BaseModel, bool>>(where.Body,where.Parameters);
}

答案1:使用:

Expression<Func<object, bool>>

支持任何类型.

示例:

Expression<Func<object, bool>> exp = p => ((Table1)p).Code == 1;
var a = new MyContext().Table1.Where(exp).ToList();

这篇关于存储表达式&lt; Func&lt; T,bool&gt;.在哪里作为阶级财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆