List.Select()方法不执行Func内部的方法. [英] List.Select() method does not execute method inside the Func<>

查看:70
本文介绍了List.Select()方法不执行Func内部的方法.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此给定示例中为什么不打印名称?

Why are the names NOT being printed in this given example?

customer.Select()方法实际上不打印数据.是真的不执行 Select()方法内的 Func<> 还是这里发生了一些不同的事情??

customer.Select() method really NOT printing the data. Is it really NOT executing the Func<> inside the Select() method OR something different is happening here??

代码:

public static void Main()
{
     var customers = new List<Customer>()
     {
         new Customer() { Name = "Bill" }, 
         new Customer() { Name = "Steve" }
    };

    customers.Select(
    c =>
    {
        Console.WriteLine("Name is {0}" + c.Name);
        return 0;
    });

    Console.Read();
}

请问有人为什么不执行或打印吗?

Would anybody please do let me know why it is NOT executing OR printing?

推荐答案

Select 不会执行任何操作,除非您枚举结果.

Select does not do anything unless you enumerate over the results.

如下面的评论中所述,这称为 @BCdotNet )

As mentioned in the comment below, this is called deferred execution (thanks @BCdotNet)

您可能正在寻找 ForEach 而不是 Select :

You are probably looking for ForEach instead of Select:

customers.ForEach(
    c =>
    {
        Console.WriteLine("Name is {0}" + c.Name);
    });

Select是一个投影",它将一个集合作为输入,并产生一个新的集合,当对其进行枚举时,它将在输入集合的每个元素上调用指定的委托,并且每个此类委托调用的结果就是这些元素输出集合中的内容.

Select is a "projection", it takes a collection as input, and produces a new collection that when enumerated over will call the specified delegate on each element of the input collection, and the result from each such delegate call is the elements of the output collection.

由于您只说过以后再枚举,这就是您应该做的",然后再从不枚举,所以什么也没有发生.

Since you only said "When you later enumerate over this, this is what you should do", and then never enumerated over it, nothing happened.

Select 更像是如何收集一个集合并创建另一个集合的食谱".在执行配方(对它进行枚举)之前,配方就是那个配方.

Select is thus more a "recipe" of how to take one collection and create another. Until you execute the recipe (enumerating over it), the recipe is just that, a recipe.

说了这么多,除非您实际上需要使用Select,否则您应该这样做:

Having said all that, unless you actually need it to use Select, this is what you should do:

foreach (var c in customers)
    Console.WriteLine("Name is {0}" + c.Name);

另一方面,如果您真的需要它来使用Select ,则可以使用以下方式枚举它:

On the other hand, if you really need it to use Select, you can use the following ways to enumerate over it:

  1. 以(我认为)适用性的顺序调用将强制枚举以完成其工作"的众多方法之一:

  1. Call one of the many methods that will "force enumeration to do its job", in order of (my opinionated) applicability:

  • LastOrDefault
  • ToList
  • ToArray

示例:

customers.Select(
    c =>
    {
        Console.WriteLine("Name is {0}" + c.Name);
        return 0;
    }).LastOrDefault();

  • 只需简单地枚举它即可:

  • Just plain enumerate over it:

    foreach (var dummy in customers.Select(
        c =>
        {
            Console.WriteLine("Name is {0}" + c.Name);
            return 0;
        }))
    { } // do nothing here, empty loop body
    


  • 最后,您错误地使用了 Console.WriteLine ,您添加了一个参数,但是随后使用字符串串联来构建结果字符串,这意味着您将看到:


    Lastly, you're using Console.WriteLine incorrectly, you've added a parameter, but then use string concatenation to build the resulting string, meaning you will see this:

    Name is {0}Bill
    Name is {0}Steve
    

    相反,这是我的最终建议:

    Instead, here's my final advice:

    foreach (var c in customers)
        Console.WriteLine("Name is " + c.Name);
    

    这篇关于List.Select()方法不执行Func内部的方法.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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