C# Error CS0834 – A lambda expression with a statement body cannot be converted to an expression tree

C# Error

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

Reason for the Error & Solution

A lambda expression must have an expression body to be converted to an expression tree.

Lambdas that are translated to expression trees must be expression lambdas; statement lambdas and anonymous methods can only be converted to delegate types.

To correct this error

  1. Remove the statement from the lambda expression.

Example

The following example generates CS0834:

// cs0834.cs  
using System;  
using System.Linq;  
using System.Linq.Expressions;  
  
public class C  
{  
    public static int Main()  
    {  
        Expression<Func<int, int>> e = x => { return x; }; // CS0834  
    }  
}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...