简体   繁体   English

如何翻译此 C# 示例中的 Lambda 运算符函数?

[英]How can I translate the Lambda operator functions in this C# example?

I am trying to understand this block of code:我试图理解这段代码:

List<string> selectedValues = CheckBoxList1.Items.Cast<ListItem>()
    .Where(li => li.Selected)
    .Select(li => li.Value)
    .ToList();

From my research I understand the point of the Lambda function (to provide a function that can be used as a parameter in-line) but why in this case is it being used?从我的研究中,我理解 Lambda 函数的意义(提供一个可以用作内嵌参数的函数),但为什么在这种情况下使用它?

To add only items to selectedValues that are li.Selected, and only add the value of those selected items to the list called selectedValues?仅将项目添加到 li.Selected 的 selectedValues,并且仅将这些选定项目的值添加到名为 selectedValues 的列表中? If this is the case, is there no other more readable way to do so?如果是这种情况,有没有其他更易读的方法来做到这一点? How else could this be written?这还能怎么写? Is my understanding correct?我的理解正确吗?

Thanks for your time.谢谢你的时间。

The block of code in questions does the following:问题中的代码块执行以下操作:

  1. Get all items in CheckBox1.Items获取CheckBox1.Items所有项目
  2. Cast them to the type of ListItem将它们转换为ListItem的类型
  3. Filter for only the items that are selected仅过滤选定的项目
  4. Select the value of those, now filtered, items.选择那些现已过滤的项目的值。
  5. Cast the result to a list.将结果转换为列表。

You could also write the code as follows:您也可以编写如下代码:

List<string> selectedValues = new List<string>();
foreach (object item in CheckBoxList1.Items) {
    var listItem = (ListItem)item;
    if (listItem.Selected) {
       selectedValues.Add(listItem.Value);
    }
 }

You did read the lambda correctly.您确实正确阅读了 lambda。 lambda are easier to get if you think of it as a step by step execution.如果您将 lambda 视为逐步执行,则更容易获得。 To me they are more readable and they are all coherent.对我来说,它们更具可读性,而且它们都是连贯的。 They follow the same structure all the way while there are many ways to write the same thing, the lambda for this example will always be the same.它们一直遵循相同的结构,虽然有很多方法可以编写相同的内容,但此示例的 lambda 将始终相同。

Another way to write it (note that here i code everything the lambda actually verifies) :另一种编写它的方法(请注意,我在这里编写了 lambda 实际验证的所有内容):

List<string> selectedValues = new List<string>();

for(int i = 0; i < CheckBoxList1.Items.Count(); i++)
{
    ListItem item = CheckBoxList1.Items[i] as ListItem;

    if(item != null)
    {
        if(item.Selected == true)
        {
            selectedValues.Add(item.Value);
        }
    }
}

Your code is pretty expressive about what it should do or doing.您的代码对于它应该做什么或做什么非常具有表现力。

Lambda expression is just a delegate pointing to an anonymous method which a method with body but it has no name using which it can be invoked. Lambda 表达式只是一个指向匿名方法的委托,该方法具有主体但没有可以调用的名称。

When we write the following:当我们编写以下内容时:

CheckBoxList1.Items.Cast<ListItem>().Where(x => x.Selected)

Compiler would generate a method like following:编译器将生成如下方法:

internal bool <M>b__0_0(ListItem x)
{
    return x.Selected;
}

and a delegate type to hold reference to that method :和一个委托类型来保存对该方法的引用:

public static Func<ListItem, bool> <>9__0_0;

and will call the method for each item in the collection.并将为集合中的每个项目调用该方法。

We can rewrite the code without using lambda expression for understanding like :我们可以在不使用 lambda 表达式的情况下重写代码,例如:

public bool IsSelectedWhere(ListItem item)
{
    return item.Selected;
}

public object SelectValue(ListItem item)
{
    return item.Value;
}

and we can call like:我们可以这样调用:

 CheckBoxList1.Items.Cast<ListItem>().Where(IsSelectedWhere)
.Select(SelectValue)
.ToList();

The benefit of lambda expression is that we don't need to define methods in the code and we just write them as expression and compiler takes care of generating a method and calling it using Func<T> . lambda 表达式的好处是我们不需要在代码中定义方法,我们只需将它们编写为表达式,编译器负责生成一个方法并使用Func<T>调用它。

Hope it helps to some extent.希望它在某种程度上有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM