从现有 Lambda 表达式创建动态 Lambda
我有一个扩展方法,可以配置 Telerik 网格的过滤。它接收 lambda 表达式作为参数。是否可以从现有的表达式中创建新的表达式,例如
public static void ConfigureFiltering<T>(this HtmlHelper html, Configurator conf, params Expression<Func<T,object>>[] args) where T:class
{
}
我想创建表达式,例如
Expression<Func<object,bool?>> filtere = obj=>obj == null? null: obj.ToString().StartsWith("xyz");//return type is nullable cause of string
Expression<Func<object,bool>> filtere = obj=>Convert.ToInt32(obj) < 20 //return type is non-nullable cause of int
有人可以指导我如何解决这个问题吗
i have an extension method that configures the filtering for telerik grid. it receives lambda expressions as parameter. is it possible to make new expressions from existing ones e.g
public static void ConfigureFiltering<T>(this HtmlHelper html, Configurator conf, params Expression<Func<T,object>>[] args) where T:class
{
}
i want to create expressions like
Expression<Func<object,bool?>> filtere = obj=>obj == null? null: obj.ToString().StartsWith("xyz");//return type is nullable cause of string
Expression<Func<object,bool>> filtere = obj=>Convert.ToInt32(obj) < 20 //return type is non-nullable cause of int
can someone plz guide me how to target this problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您遇到的问题是什么,也不知道您问题的第一部分和第二部分有何关联。
我可以告诉你,第一个表达式中的三元运算符需要将
null
转换为bool?
,因此它将变成:此外,两个表达式不能共享相同的变量
filtere
的名称。除此之外,您需要更详细地解释您想要做什么。
I'm not sure what the problem is that you're having, nor how the first and second parts of your question relate.
I can tell you that the ternary operator in your first expression will need to cast that
null
tobool?
, so it will become:Also, both expressions cannot share the same variable name of
filtere
.Beyond that, you'll need to explain in somewhat more detail what you are trying to do.