在lambda表达式中调用函数

时间:2015-01-29 12:07:42

标签: c# function lambda

在C#中,是否可以在lambda表达式中调用函数?

例如,以下是lambda表达式的一些内容:

(a => a.testBool && a.testBool2)

是否可以将上述内容写入函数,并调用lambda表达式中的函数以获得相同的结果?

提前致谢。

修改

在我的原帖中,我没有声明这是针对dbSet的。

我在测试对象(a)中创建了以下函数,如下所示:

public bool testBool()
{
    return true;
}

调用以下代码时:

(a => a.testBool()).ToList()

我收到此错误:

  

LINQ to Entities无法识别方法'布尔testBool()'   方法,并且此方法无法转换为商店表达式。

1 个答案:

答案 0 :(得分:-1)

当然,如果它是在类a。

中声明的函数
public class a
{
    bool testBool()
    {
     ...
    }

    bool testBool2()
    {
     ...
    }
}

然后你可以这样做:

ListA.Where(a => a.testBool() && a.testBool2()).ToList();