Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# compiler error: Cannot convert lambda expression

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.

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:

string fullname = GetExpressionText(model => model.Locations.PreferredAreas);
"Locations.PreferredAreas" == fullname;

Instead I get a compile error:

Error 4: Cannot convert lambda expression to type 'System.Linq.Expressions.LambdaExpression' because it is not a delegate type.

Why does the parameter work in the second case below, but not the first?

// This doesn't compile:
string tb1 = System.Web.Mvc.ExpressionHelper.
    GetExpressionText(model => model.Locations.PreferredAreas);

// But this does:
MvcHtmlString tb2 =
    Html.TextBoxFor(model => model.Locations.PreferredAreas);

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:

// MVC extension method
public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> 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...
like image 511
shannon Avatar asked Mar 15 '11 05:03

shannon


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

The error message is correct. A lambda can be converted to a compatible delegate type, D, or to an expression-of-compatible-delegate-type Expression<D>. Expression<Func<TM, TP>> is one of those. "LambdaExpression" is neither of those. Therefore you get an error trying to convert the lambda to LambdaExpression, but not to an actual expression tree type. There has to be a delegate in there somewhere.

like image 76
Eric Lippert Avatar answered Sep 30 '22 01:09

Eric Lippert


Before trying to fix the lambda expressions, be sure that the following references have already been added:

System.Linq;
System.Linq.Expressions;

The lack of these references may cause the same error as well ("Cannot convert lambda expression to type 'System.Linq.Expressions.Lambda Expression' because it is not a delegate type").

Hope this helps...

like image 34
Murat Yıldız Avatar answered Sep 29 '22 23:09

Murat Yıldız