Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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
Linq 检索lambda表达式的值_Linq_Lambda_Expression - Fatal编程技术网

Linq 检索lambda表达式的值

Linq 检索lambda表达式的值,linq,lambda,expression,Linq,Lambda,Expression,我有这个功能: public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> exp1, Expression<Func<TSource, bool>> exp2, Expression<Func<TSource, bool>&

我有这个功能:

public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> exp1, Expression<Func<TSource, bool>> exp2, Expression<Func<TSource, bool>> exp3)
    {


    }
如何获取exp1的值


在扩展中,我需要知道DeactivatedDate.HasValue是否为true

可以对表达式求值,但需要使用
TSource
的实例进行求值。由于您的“输入”是
TSource
的集合,您的解决方案可能类似于:

foreach(TSource t in source)
{
    bool isTrue = exp1.Compile()(t);  //  evaluate exp1 for this item
    if(isTrue)
        // do something
    else
        // do something else
} 

但是请注意,您的返回类型为
IQueryable
-因此我不确定您是否已经充分考虑了如何构建结果查询…

此函数对我来说没有任何意义。我不明白-
exp1
是您传递给
的一个表达式,如果
,那么它的“值”是
x=>x.DeactivatedDate.HasValue
。您的意思是要针对某个对象计算该表达式并查看它返回的值吗?问题是我需要在where()子句中使用if条件。正因为如此,我创建了一个this扩展,如果你有建议,我将非常感谢知道。我想获得该扩展中DeactivatedDate.HasValue的值。在扩展中,我需要知道DeactivatedDate.HasValue是否为真,做点什么。在这一行中,我有一个问题bool isTrue=exp1(t);它不接受exp1(t);现在它起作用了。正如您所看到的,我的扩展有一个输出(IQueryable),我如何在源代码上应用我的条件并将其作为IQueryable发送回去?
foreach(TSource t in source)
{
    bool isTrue = exp1.Compile()(t);  //  evaluate exp1 for this item
    if(isTrue)
        // do something
    else
        // do something else
}