title: C# compiler error: Cannot convert lambda expression
body: <p>I'm trying to use a Lambda expression and reflection to get a member hierarchical name (rather than using a text constant), to enforce compile-time errors if my control binding information is invalid.</p>
  
  <p>This is in an ASP.NET MVC project, but it's not an MVC-specific question AFAIK. EDIT: Specifically, I want the following to evaluate to true:</p>
  
  <pre><code>string fullname = GetExpressionText(model =&gt; model.Locations.PreferredAreas);
  "Locations.PreferredAreas" == fullname;
  </code></pre>
  
  <p>Instead I get a compile error:</p>
  
  <blockquote>
    <p>Error 4: Cannot convert lambda expression to type
    'System.Linq.Expressions.LambdaExpression' because it is not a delegate type.</p>
  </blockquote>
  
  <p>Why does the parameter work in the second case below, but not the first?</p>
  
  <pre><code>// This doesn't compile:
  string tb1 = System.Web.Mvc.ExpressionHelper.
      GetExpressionText(model =&gt; model.Locations.PreferredAreas);
  
  // But this does:
  MvcHtmlString tb2 =
      Html.TextBoxFor(model =&gt; model.Locations.PreferredAreas);
  </code></pre>
  
  <p>Here's the relevant code from the ASP.NET MVC Codeplex project. It looks to me like it passes the same parameter through to the same method:</p>
  
  <pre><code>// MVC extension method
  public static MvcHtmlString TextBoxFor&lt;TModel, TProperty&gt;(this HtmlHelper&lt;TModel&gt; htmlHelper, Expression&lt;Func&lt;TModel, TProperty&gt;&gt; expression, IDictionary&lt;string, object&gt; htmlAttributes) {
      ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
      return TextBoxHelper(
          htmlHelper,
          metadata,
          metadata.Model,
          ExpressionHelper.GetExpressionText(expression),
          htmlAttributes);
  }
  
  // MVC utility method
  public static string GetExpressionText(LambdaExpression expression) {
      // Split apart the expression string for property/field accessors to create its name
      // etc...
  </code></pre>