Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to understand the lambda expression in @Html.DisplayFor(modelItem => item.FirstName)

I’m fairly new at C# and MVC and have used lambdas on certain occasions, such as for anonymous methods and on LINQ.

Usually I see lambda expressions that look something like this:

(x => x.Name), (x => { Console.WriteLine(x)) 

I understand that lambda = "goes to". I have never seen a lambda expression where the left parameter is not used.

I don’t know how to translate this lambda expression though

@Html.DisplayFor(modelItem => item.FirstName) 

Can anyone shed some light on this one for me? Shouldn’t this be

(modelItem => modelItem.FirstName)? 

I got this from Microsoft's Introduction to ASP.NET MVC tutorial.

like image 825
Jan Carlo Viray Avatar asked Apr 10 '12 23:04

Jan Carlo Viray


People also ask

What does HTML DisplayFor do?

DisplayFor<TModel,TValue>(HtmlHelper<TModel>, Expression<Func<TModel,TValue>>, String, String, Object) Returns HTML markup for each property in the object that is represented by the specified expression, using the template, an HTML field ID, and additional view data.

Why use lambda expressions?

Easy-to-Use APIs and Libraries: An API designed using lambda expressions can be easier to use and support other API. Enables support for parallel processing: A lambda expression can also enable us to write parallel processing because every processor is a multi-core processor nowadays.

What is the use of lambda expression in java?

A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

How do you declare a lambda expression?

To create a lambda expression, you specify input parameters (if any) on the left side of the lambda operator and an expression or a statement block on the other side. When you use method-based syntax to call the Enumerable. Select method in the System.


2 Answers

A lambda expression is a way to write an anonymous function, i.e. a function without a name. What you have on the left side of the "arrow" are the function parameters, and what you have on the right side are the function body. Thus, (x => x.Name) logically translates to something like string Function(Data x) { return x.Name } the types string and Data will obviously vary and be derived from the context.

The absence of the left-side parameter translates into a function without parameters, so that this (() => someVariable) logically translates to this: string Function() { return someVariable; }

At this point you might start wondering, where someVariable comes from, it's not a function parameter and it is not defined in the function. You would be correct, a function like this would never compile. However the lambda function like this is perfectly fine, as it allows outer-scope variables be lifted and used this way. (Internally a wrapper class is created where the variables that are used in the lambda expression become fields.)

Now let's see what model => item.FirstName means. Logically it would translate to string Function(Model model) { return item.FirstName; }. Basically this is a function with a parameter, but this parameter is not used.

And now, the last bit of the information. Although lambda expressions represent functions in the end, sometimes they are created not with the purpose of actually being executed (although potentially they can). A lambda expression can be represented in the form of an expression tree. This means that instead of executing it it can be parsed.

In this particular case the MVC engine does not run the function that the lambda expression represents. Instead the expression is parsed so that MVC engine knows what html to emit for this particular line. If your data item does not come from your model object directly, the left part of the lambda expression does not matter.

like image 189
Andrew Savinykh Avatar answered Oct 13 '22 02:10

Andrew Savinykh


i think it's about the foreach loop. example:

@foreach(var item in model)  {   <td>       @html.displayfor(model => item.firstName) </td>  </td>      } 

var item needs to be used because each item in the sequence is an anonymous type. model => item.firstName means (input parameter) => expression. you can't use the input parameter because we store the current "item" in item.

like image 44
varg Avatar answered Oct 13 '22 02:10

varg