C# 存储表达式<;Func<;T、 布尔>&燃气轮机;作为类属性的位置
我有一个扩展System.Web.UI.WebControl.GridView控件的类。 我想有一个属性,可以保存我的EF表达式以在整个控件中使用 问题是,T没有定义C# 存储表达式<;Func<;T、 布尔>&燃气轮机;作为类属性的位置,c#,asp.net,.net,entity-framework,C#,Asp.net,.net,Entity Framework,我有一个扩展System.Web.UI.WebControl.GridView控件的类。 我想有一个属性,可以保存我的EF表达式以在整个控件中使用 问题是,T没有定义 public sealed class NCGridView : GridView { private Expression<Func<T, bool>> _where; public void LoadWhere(Expression<Func<T, bool>>
public sealed class NCGridView : GridView
{
private Expression<Func<T, bool>> _where;
public void LoadWhere(Expression<Func<T, bool>> where)
{
_where = where;
}
}
公共密封类NCGridView:GridView
{
私人表达(where);;
公共void LoadWhere(表达式where)
{
_where=where;
}
}
红帽建议尝试
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;
}
private Expression\u其中;
公共void LoadWhere(表达式where)where T:BaseModel
{
//无法从:表达式强制转换为:表达式
_where=where;
}
答案2:
更新:
公共表达式LoadWhere(表达式where)where T:BaseModel
{
其中=LambdaExpression.Lambda(where.Body,where.Parameters);
}
答复1:
使用:
表达式
支持任何类型
样本:
Expression<Func<object, bool>> exp = p => ((Table1)p).Code == 1;
var a = new MyContext().Table1.Where(exp).ToList();
表达式exp=p=>((表1)p);
var a=new MyContext().Table1.Where(exp.ToList();
嗯。。。T代表什么?将is存储为非泛型表达式就足够了吗?(在需要和已知时强制转换)T是绑定到GridView的行的类型。你能在设计时知道这一点吗?我从来没有听说过通用控件。不,等等,看看。你能举个例子说明你打算如何使用这个表达吗?@Marc ha我在我的编辑中偷看了一下deadline@dtb我的总体目标是拥有一个扩展的GridView控件,它将与我的POCO实体一起工作。因此,我可以告诉gridview我正在使用哪个实体集,它将知道如何在内部使用它们。我对它进行了测试,将添加一些示例。@RedHat,我认为使用泛型类型与使用基类是我在本例中的问题。是的,你说得对,但是@Marc Gravell在问题注释“IDE讨厌它们(泛型)”那么,将gridview扩展到从BaseModel派生的所有模型的抽象默认行为的最佳实践是什么呢?
Expression<Func<object, bool>>
Expression<Func<object, bool>> exp = p => ((Table1)p).Code == 1;
var a = new MyContext().Table1.Where(exp).ToList();