A lambda expression with a statement body cannot be converted to an expression tree

Guillermo Perez 26 Reputation points
2021-02-03T04:19:33.86+00:00

Thank you for reading. I have a question that is breaking my mind... I do have a table in my database and I was thinking on filtering the records using WHERE (linq), the only thing is that I would like to filter using a function that return true/false according to what it finds... example:

var myrecords = db.Locations.Where(w => {
                        **//some code here...
                        //some code here...
                        return false;** 
                    });

According to my understanding, it requires a Func<location, bool> that takes a location (in this case, wherever the type is) and returns a bool in order to include or not the record... the code above fails saying: "A lambda expression with a statement body cannot be converted to an expression tree"

Now, if I create the Func and then call it inside the parenthesis, then I can include multi-line...

Func<Location, bool> processingFunc = location =>
                    {
                        //some code...
                        //some code...
                        return true;
                    };



var x = db.Locations.Where(location => processingFunc(location));

my question is: If the FUNC "processingFunc" I created is the same as the code block, why I can't do the block where the WHERE is? am I missing something here? in other words, if I create a separated FUNC, I can add múltiple lines of code and return the desired boolean, but if I try to do it in the WHERE parenthesis then I cant...

Thank you for your help :-)

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,095 questions
0 comments No comments
{count} vote

3 answers

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-02-04T05:40:18.99+00:00

    The error message is telling you that Linq-2-Entites cannot take your created function and generate T-SQL that can be submitted to the database engine to be executed against a database.


  2. Duane Arnold 3,216 Reputation points
    2021-02-05T00:34:50+00:00

    I want to make an anonymous block that returns a boolean I can't do it BUT if I point to an external FUNC it do work, what difference makes if I create an anonymous block of code or an external func if both do the same?

    You can call a method that helps build the Linq projection for an anaymous type or a known type within the projection for a property or an object within the type, which is a projection of the results returned from the Linq query.

    0 comments No comments

  3. Karen Payne MVP 35,456 Reputation points
    2021-06-22T21:07:41.29+00:00

    Hello,

    Try the following, wrap an Expression around the Func

    Expression<Func<Locations, bool>> processingFunc  = p => p.City == "Portland";
    var myrecords = db.Locations.Where(processingFunc).Count();
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.