我想了解在@ Html.DisplayFor拉姆达前pression(modelItem => item.FirstName) [英] I want to understand the lambda expression in @Html.DisplayFor(modelItem => item.FirstName)

查看:401
本文介绍了我想了解在@ Html.DisplayFor拉姆达前pression(modelItem => item.FirstName)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#和MVC的相当新的并且对某些场合lambda表达式,如用于匿名的方法和对LINQ

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

通常我看到拉姆达前pressions看起来是这样的:

Usually I see lambda expressions that look something like this:

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

据我所知,波长=变为。我从来没有见过一个lambda前pression在不使用左参数。​​

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

我不知道如何翻译这个拉姆达前pression虽然

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)?

我得到这个从微软的<一个href=\"http://www.asp.net/mvc/tutorials/mvc-5/introduction/accessing-your-models-data-from-a-controller\">Introduction到ASP.NET MVC教程。

推荐答案

一个拉姆达前pression是写一个匿名函数的方式,即没有名称的函数。你对箭的左侧有什么函数的参数,你必须在右侧哪些函数体。因此,(X =&GT; x.Name)逻辑转换为类似于字符串函数(数据X){返回x.Name} 的类型字符串数据将明显变化,从上下文的。

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.

缺少左侧的参数转换成不带参数的函数,因此,这个(()=&GT; someVariable)逻辑转化为这样:字符串函数(){返回someVariable; }

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; }

在这一点上,你可能会开始怀疑,其中 someVariable 从何而来,它不是一个函数参数,它是不是在函数中定义。你会是正确的,这样的功能永远不会编译。然而这样的lambda函数是完全正常的,因为它允许外部范围的变量被解除,并使用这种方式。 (内部包装类创建,其中那些在拉姆达前pression使用的变量成为各个领域。)

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.)

现在让我们来看看模式=&GT; item.FirstName 表示。按道理它会转换为字符串函数(型号模型){返回item.FirstName; } 。基本上,这是带有参数的功能,但不使用该参数。

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.

现在,该信息的最后一位。虽然拉姆达前pressions重新present功能到底,有时它们不与实际执行的目的而创建(尽管可能他们可以)。一个lambda前pression可以在一个前pression树的形式psented重新$ P $。这意味着,而不是执行它也可以是解析

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 form of an expression tree. This means that instead of executing it it can be parsed.

在这种特殊情况下MVC发动机没有的运行功能的兰巴前pression重新presents。取而代之的是前pression是的解析<​​/ em>的使MVC引擎知道HTML发出了这一行。如果您的数据项目不从你的模型对象直接上门,拉姆达前pression的左侧部分没有关系。

In this particular case MVC engine does not run the function that the lamba 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.

这篇关于我想了解在@ Html.DisplayFor拉姆达前pression(modelItem =&GT; item.FirstName)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆