Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从现有Lambda表达式创建动态Lambda_C#_Linq_Lambda_Expression Trees - Fatal编程技术网

C# 从现有Lambda表达式创建动态Lambda

C# 从现有Lambda表达式创建动态Lambda,c#,linq,lambda,expression-trees,C#,Linq,Lambda,Expression Trees,我有一个扩展方法,为telerik网格配置过滤。它接收lambda表达式作为参数。是否可以从现有表达式中生成新表达式,例如 public static void ConfigureFiltering<T>(this HtmlHelper html, Configurator conf, params Expression<Func<T,object>>[] args) where T:class { } publicstaticvoidconfig

我有一个扩展方法,为telerik网格配置过滤。它接收lambda表达式作为参数。是否可以从现有表达式中生成新表达式,例如

public static void ConfigureFiltering<T>(this HtmlHelper html, Configurator conf, params Expression<Func<T,object>>[] args) where T:class 
{
 }   
publicstaticvoidconfigurefiltering(此htmlhelp-html,Configurator-conf,params-Expression[]args),其中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
Expression filtere=obj=>obj==null?null:obj.ToString().StartsWith(“xyz”)//由于字符串的原因,返回类型可为null
表达式filtere=obj=>Convert.ToInt32(obj)<20//返回类型是int的不可空原因

有人能指导我如何针对这个问题吗?我不知道你的问题是什么,也不知道你问题的第一部分和第二部分是如何联系的

我可以告诉您,第一个表达式中的三元运算符需要将
null
转换为
bool?
,因此它将变成:

Expression<Func<object,bool?>> filtere = obj=>obj == null
    ? (bool?)null 
    : obj.ToString().StartsWith("xyz");
Expression filtere=obj=>obj==null
? (bool?)空
:obj.ToString().StartsWith(“xyz”);
此外,两个表达式不能共享相同的变量名
filtere


除此之外,你还需要更详细地解释你想做什么。

这是可以做到的;您是否有一个源表达式的示例以及您希望将其转换为什么?没有。我第一次尝试创建一个源表达式,但不知道从何处开始,args是类型为
expression
的源lambda数组,我希望将它们转换为第二个代码段中的代码,而不是我方法的主体。它是我想从
expression
开始实现的目标表达式的示例。目标是创建一个类型为
expression
的表达式,如果对象可为空,则为
expression