C# 我想了解@Html.DisplayFor(modelItem => item.FirstName) 中的 lambda 表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10097995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I want to understand the lambda expression in @Html.DisplayFor(modelItem => item.FirstName)
提问by Jan Carlo Viray
I'm fairly new at C# and MVC and have used lambdas on certain occasions, such as for anonymous methods and on LINQ.
我是 C# 和 MVC 的新手,在某些场合使用过 lambda,例如匿名方法和 LINQ。
Usually I see lambda expressions that look something like this:
通常我会看到如下所示的 lambda 表达式:
(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.
我知道 lambda = "goes to"。我从未见过未使用 left 参数的 lambda 表达式。
I don't know how to translate this lambda expression though
我不知道如何翻译这个 lambda 表达式
@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.
我从微软的ASP.NET MVC 教程简介中得到了这个。
采纳答案by Andrew Savinykh
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 stringand Datawill obviously vary and be derived from the context.
lambda 表达式是一种编写匿名函数的方法,即没有名称的函数。“箭头”左侧是函数参数,右侧是函数体。因此,(x => x.Name)逻辑上转换为类似string Function(Data x) { return x.Name }类型的东西,string并且Data显然会有所不同并从上下文中得出。
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)逻辑上转换为:string Function() { return someVariable; }
At this point you might start wondering, where someVariablecomes 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.)
此时您可能开始想知道,它someVariable是从哪里来的,它不是函数参数,也没有在函数中定义。你是对的,这样的函数永远不会编译。然而,像这样的 lambda 函数非常好,因为它允许以这种方式提升和使用外部作用域变量。(在内部创建了一个包装类,其中 lambda 表达式中使用的变量成为字段。)
Now let's see what model => item.FirstNamemeans. 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.
现在让我们看看是什么model => item.FirstName意思。从逻辑上讲,它会转换为string Function(Model model) { return item.FirstName; }. 基本上这是一个带参数的函数,但是没有使用这个参数。
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.
现在,信息的最后一点。尽管 lambda 表达式最终代表了函数,但有时创建它们并不是为了实际执行(尽管它们可能可以)。lambda 表达式可以用表达式树的形式表示。这意味着它可以被解析而不是执行它。
In this particular case the MVC engine does not runthe function that the lambda expression represents. Instead the expression is parsedso 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.
在这种特殊情况下,MVC 引擎不运行lambda 表达式表示的函数。相反,表达式会被解析,以便 MVC 引擎知道要为此特定行发出什么 html。如果您的数据项不是直接来自您的模型对象,则 lambda 表达式的左侧部分无关紧要。
回答by GraemeMiller
It is using a parameterless lambada. See this question
它正在使用无参数的 lambda。看到这个问题
Basically DisplayFor doesn't use the lambda function parameter model (it could be anything I'd say use _ or ()) and just uses the lambda function within the for loop to use displayfor against it. DisplayFor requires a lambda function.
基本上 DisplayFor 不使用 lambda 函数参数模型(它可以是我所说的任何使用 _ 或 ()),而只是在 for 循环中使用 lambda 函数来针对它使用 displayfor 。DisplayFor 需要一个 lambda 函数。
回答by varg
i think it's about the foreach loop. example:
我认为这是关于 foreach 循环。例子:
@foreach(var item in model)
{
<td>
@html.displayfor(model => item.firstName) </td>
</td>
}
var itemneeds to be used because each item in the sequence is an anonymous type.
model => item.firstNamemeans (input parameter) => expression. you can't use the input parameter because we store the current "item" in item.
var item需要使用,因为序列中的每个项目都是匿名类型。
model => item.firstName是指(input parameter) => expression。您不能使用输入参数,因为我们将当前“项目”存储在item.
回答by Peter Chung
I also struggled a lot to understand the codes generated by Visual Studio. Instead of providing a general explanation about lambda expression, I would like to put a context using ASP.NET MVC framework.
我也很难理解 Visual Studio 生成的代码。我不想提供有关 lambda 表达式的一般解释,而是想放置一个使用 ASP.NET MVC 框架的上下文。
Suppose we prepared a Model class (e.g. Destination) with 2 attributes: City and ProvinceCode.
假设我们准备了一个具有 2 个属性的模型类(例如 Destination):City 和 ProvinceCode。
public class Destination
{
public string City { get; set; }
public string ProvinceCode { get; set; }
}
After generating the Controller and View, we should get the generated codes by Visual Studio as mentioned. Yet, the generated codes are somewhat hard to understand, especially for the data rows
生成控制器和视图后,我们应该通过 Visual Studio 获取生成的代码。然而,生成的代码有点难以理解,尤其是对于数据行
@Html.DisplayFor(modelItem => item.City)
I just guessthat the MVC team should think that Html helper class should be consistently used in the cshtml file. Thus, they tried to use tricks to pass the C# compiler. In this case, modelItem is not even used as an input parameter of this lambda expression. We can't use () as the type is NOTcorrect. That is why, if we replace modelor any model object, the lambda expression works.
我只是猜测MVC团队应该认为在cshtml文件中应该一致地使用Html helper类。因此,他们试图使用技巧来通过 C# 编译器。在这种情况下,modelItem 甚至不用作此 lambda 表达式的输入参数。我们不能使用 () 因为类型不正确。这就是为什么,如果我们替换模型或任何模型对象,lambda 表达式会起作用。
To be honest, I would like to rewrite the generated codes in a more readable form. Instead of using the Htmlhelper class, we can simply render the correct output as follows:
老实说,我想以更具可读性的形式重写生成的代码。我们可以简单地呈现正确的输出,而不是使用Html帮助器类,如下所示:
@foreach (var item in Model) {
<tr>
<td>
@* Code Generated by Visual Studio. modelItem is a dummy param *@
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@* A better way - simply get rid of Html helper class *@
@item.ProvinceCode
</td>
</tr>
}

