简体   繁体   English

商店表达 <Func<T, bool> &gt;作为类的属性

[英]store Expression<Func<T, bool>> where as a class property

I have a class that extends the System.Web.UI.WebControls.GridView control. 我有一个扩展System.Web.UI.WebControls.GridView控件的类。 I want to have a property that can save my EF expression to use throughout the control. 我想拥有一个可以保存我的EF表达式以在整个控件中使用的属性。

Problem is, T is not defined. 问题是,T没有定义。

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

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

RedHat suggestion attempt RedHat建议尝试

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

Answer 2: Update: 答案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);
}

Answer 1: Use: 答案1:使用:

Expression<Func<object, bool>>

Supported any type. 支持任何类型。

Sample: 样品:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Where(表达式 <Func<T, bool> &gt;)在IGrouping中 - Using Where( Expression<Func<T, bool>> ) in IGrouping 如何存储表达式<t, bool>其中 T:将 CustomClass 转换为表达式<customclass, bool>财产?</customclass,></t,> - How can I store an Expression<T, bool> where T : CustomClass into an Expression<CustomClass, bool> property? 将基于属性名称和值的过滤器表达式转换为 Linq Where 子句 Func<T, bool> ? - Convert a filter expression based on property name and value to Linq Where clause Func<T, bool>? 无法从表达式转换<func<t,bool> &gt; 到表达式<func<user,bool> &gt; 当 T = class 内的用户时</func<user,bool></func<t,bool> - Cannot convert from Expression<Func<T,bool>> to Expression<Func<User,bool>> when T = User within class 表达<Func<object, bool> &gt; 作为财产 - Expression<Func<object, bool>> as Property 转换表达式 <Func<T,T,bool> &gt;表达 <Func<T,bool> &gt; - Convert Expression<Func<T,T,bool>> to Expression<Func<T,bool>> 表达式<Func <T,bool >>的Moq'ing方法作为参数传入 - Moq'ing methods where Expression<Func<T, bool>> are passed in as parameters 联接表达 <Func<T,bool> &gt; - Join Expression<Func<T,bool>> 扩展表达式 <func<t, bool> &gt; - Extending an expression<func<t, bool>> 修改表达 <Func<T, bool> &gt; - Modifying Expression<Func<T, bool>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM