-2

In C#, is it possible to call a function inside of a lambda expression?

For example, here are some contents of lambda expression:

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

Is it possible to write the above contents into a function, and call the function inside of the lambda expression to get the same results?

Thanks in advance.

EDIT

In my original post, I did not state that this is for a dbSet.

I have created the following function in the test object (a) as follows:

public bool testBool()
{
    return true;
}

When calling the following code:

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

I am getting this error:

LINQ to Entities does not recognize the method 'Boolean testBool()' method, and this method cannot be translated into a store expression.

Simon
  • 7,991
  • 21
  • 83
  • 163
  • 1
    ofcourse you can call a method from a lamda, lambdas are also methods.there is nothing special about them. so you can do anything you do in a normal method. – Selman Genç Jan 29 '15 at 12:12
  • Have you tried to write the above contents into a function, and call the function inside of the lambda? – Dave Becker Jan 29 '15 at 12:12
  • Are you using the lambda to create a delegate, or to create an expression tree? If you're trying to create a delegate, it's easy. If you're trying to create an expression tree, it's still possible, but a lot more complicated. –  Jan 29 '15 at 12:14

1 Answers1

0

Sure, if its a function declared in class a.

public class a
{
    bool testBool()
    {
     ...
    }

    bool testBool2()
    {
     ...
    }
}

Then you can do:

ListA.Where(a => a.testBool() && a.testBool2()).ToList();
kassi
  • 358
  • 1
  • 3
  • 10