249

In using the EntityFramework, I get the error "A lambda expression with a statement body cannot be converted to an expression tree" when trying to compile the following code:

Obj[] myArray = objects.Select(o =>
{
    var someLocalVar = o.someVar;

    return new Obj() { 
    Var1 = someLocalVar,
    Var2 = o.var2 };
}).ToArray();

I don't know what the error means and most of all how to fix it. Any help?

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
pistacchio
  • 56,889
  • 107
  • 278
  • 420

12 Answers12

155

Is objects a Linq-To-SQL database context? In which case, you can only use simple expressions to the right of the => operator. The reason is, these expressions are not executed, but are converted to SQL to be executed against the database. Try this

Arr[] myArray = objects.Select(o => new Obj() { 
    Var1 = o.someVar,
    Var2 = o.var2 
}).ToArray();
Tim Rogers
  • 21,297
  • 6
  • 52
  • 68
  • OK but what if I'm working with a single object instead of an IEnumerable? It becomes tricky : https://stackoverflow.com/questions/50837875/ienumerable-select-for-a-single-item – jeancallisti Jun 29 '22 at 08:06
128

You can use statement body in lamba expression for IEnumerable collections. try this one:

Obj[] myArray = objects.AsEnumerable().Select(o =>
{
    var someLocalVar = o.someVar;

    return new Obj() 
    { 
        Var1 = someLocalVar,
        Var2 = o.var2 
    };
}).ToArray();

Notice:
Think carefully when using this method, because this way, you will have all query results in the application's memory, that may have unwanted side effects on the rest of your code.

Amir Oveisi
  • 1,668
  • 1
  • 16
  • 25
  • 4
    +1 I like this! Adding `AsEnumerable()` maskes my problem go away! – Joel Jul 09 '13 at 11:40
  • 5
    This is the real solution, the accepted answer is difficult to apply in some cases – Ferran Salguero Aug 02 '13 at 10:47
  • 30
    No this is not the real answer. It would make your query to be executed on client side. Refer to this question for details: http://stackoverflow.com/questions/33375998/optimize-linq-to-sql-statement-that-has-foreign-key-access/33376119 – Luke Vo Oct 27 '15 at 19:09
  • 1
    @DatVM it depends on what you are going to do. this can not be always right choice and of course can not be always wrong choice. – Amir Oveisi Oct 28 '15 at 10:33
  • 4
    Though I agree with you, the OP stated that he was using EntityFramework. Most of the case, when working with EF, you want the database-side to do as much work as possible. It would be nice if you note the case in your answer. – Luke Vo Oct 28 '15 at 14:34
  • This worked for me too. Adding the AsEnumerable() function allowed me to inject the converted Var property into the underlying object without declaring "new". – Arlyn Sep 09 '16 at 22:49
  • Since nobody appears to have pointed this out... this is not a lambda expression, it's a `Func`. – Corey Jul 12 '19 at 00:02
  • Since IQueryable is already IEnumerable, do you know why the correct overload for Select - the one that accepts a delegate/Func<> - was not automatically chosen? – Social Developer May 05 '22 at 11:03
  • 1
    @AmirOveisi can you extend your "notice" sentence with the extra words "executed on client side"? I thought the words "all query result in memory" were referring to SQL db memory use. – jeancallisti Jun 29 '22 at 08:09
45

It means that you can't use lambda expressions with a "statement body" (i.e. lambda expressions which use curly braces) in places where the lambda expression needs to be converted to an expression tree (which is for example the case when using linq2sql).

sepp2k
  • 363,768
  • 54
  • 674
  • 675
8

Without knowing more about what you are doing (Linq2Objects, Linq2Entities, Linq2Sql?), this should make it work:

Arr[] myArray = objects.AsEnumerable().Select(o => {
    var someLocalVar = o.someVar;

    return new Obj() { 
        Var1 = someLocalVar,
        Var2 = o.var2 
    }; 
}).ToArray();
spender
  • 117,338
  • 33
  • 229
  • 351
7

The LINQ to SQL return object were implementing IQueryable interface. So for Select method predicate parameter you should only supply single lambda expression without body.

This is because LINQ for SQL code is not execute inside program rather than on remote side like SQL server or others. This lazy loading execution type were achieve by implementing IQueryable where its expect delegate is being wrapped in Expression type class like below.

Expression<Func<TParam,TResult>>

Expression tree do not support lambda expression with body and its only support single line lambda expression like var id = cols.Select( col => col.id );

So if you try the following code won't works.

Expression<Func<int,int>> function = x => {
    return x * 2;
}

The following will works as per expected.

Expression<Func<int,int>> function = x => x * 2;
Azri Jamil
  • 2,394
  • 2
  • 29
  • 37
6

9 years too late to the party, but a different approach to your problem (that nobody has mentioned?):

The statement-body works fine with Func<> but won't work with Expression<Func<>>. IQueryable.Select wants an Expression<>, because they can be translated for Entity Framework - Func<> can not.

So you either use the AsEnumerable and start working with the data in memory (not recommended, if not really neccessary) or you keep working with the IQueryable<> which is recommended. There is something called linq query which makes some things easier:

IQueryable<Obj> result = from o in objects
                         let someLocalVar = o.someVar
                         select new Obj
                         {
                           Var1 = someLocalVar,
                           Var2 = o.var2
                         };

with let you can define a variable and use it in the select (or where,...) - and you keep working with the IQueryable until you really need to execute and get the objects.

Afterwards you can Obj[] myArray = result.ToArray()

Matthias Burger
  • 5,549
  • 7
  • 49
  • 94
4

Use this overload of select:

Obj[] myArray = objects.Select(new Func<Obj,Obj>( o =>
{
    var someLocalVar = o.someVar;

    return new Obj() 
    { 
       Var1 = someLocalVar,
       Var2 = o.var2 
    };
})).ToArray();
Mohsen
  • 4,000
  • 8
  • 42
  • 73
  • This works for me but when used with Entity Framework would this solution prevent the dbcontext from loading all rows into memory first, like AsEnumerable() would? – parliament Nov 23 '14 at 21:57
  • 2
    @parliament:To prevent loading all rows into memory you should use `Expression>`. – Mohsen Nov 25 '14 at 05:42
2

It means that a Lambda expression of type TDelegate which contains a ([parameters]) => { some code }; cannot be converted to an Expression<TDelegate>. It's the rule.

Simplify your query. The one you provided can be rewritten as the following and will compile:

Arr[] myArray = objects.Select(o => new Obj()
                {
                   Var1 = o.someVar,
                   Var2 = o.var2
                } ).ToArray();
Harsh Baid
  • 7,199
  • 5
  • 48
  • 92
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
1

For your specific case, the body is for creating a variable, and switching to IEnumerable will force all the operations to be processed on client-side, I propose the following solution.

Obj[] myArray = objects
.Select(o => new
{
    SomeLocalVar = o.someVar, // You can even use any LINQ statement here
    Info = o,
}).Select(o => new Obj()
{
    Var1 = o.SomeLocalVar,
    Var2 = o.Info.var2,
    Var3 = o.SomeLocalVar.SubValue1,
    Var4 = o.SomeLocalVar.SubValue2,
}).ToArray();

Edit: Rename for C# Coding Convention

Luke Vo
  • 17,859
  • 21
  • 105
  • 181
1

Is Arr a base type of Obj? Does the Obj class exist? Your code would work only if Arr is a base type of Obj. You can try this instead:

Obj[] myArray = objects.Select(o =>
{
    var someLocalVar = o.someVar;

    return new Obj() 
    { 
       Var1 = someLocalVar,
       Var2 = o.var2 
    };
}).ToArray();
Atanas Korchev
  • 30,562
  • 8
  • 59
  • 93
1

As stated on other replies, you can only use simple expressions to the right of the => operator. I suggest this solution, which consists of just creating a method that does what you want to have inside of the lambda:

public void SomeConfiguration() {
    // ...
    Obj[] myArray = objects.Select(o => Method()).ToArray();
    // ..
}

public Obj Method() {
    var someLocalVar = o.someVar;

    return new Obj() { 
    Var1 = someLocalVar,
    Var2 = o.var2 };
}
Tovar
  • 99
  • 2
  • 6
-1

If you came here because this is a top google result for this error but you aren't using a list, you can always do something quick and dirty like this:

my original code:

RuleFor(m => m.DocumentName).Must( ...etc...

I want to drill into m and I know m is of type FileUploadDTO and in the above expression it is trying to return a string so I add the following method that I can set a breakpoint on:

private string GetIt(FileUploadDTO dto)
{
    return dto.FileName;
}

Then:

RuleFor(m => GetIt(m)).Must( ...etc...
Post Impatica
  • 14,999
  • 9
  • 67
  • 78