小编Cas*_*asi的帖子

.net-core 中的 linq 语句导致 IEnumerable 和 List 之间出现意外差异?

当我在 Select() 语句的末尾添加 .ToList() 时,它按预期工作。在这种情况下,IEnumerable 和 List 之间有什么区别?

class Foo{
    public string Name { get; set; }
    public Guid Id { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        var list = new List<string>(){ "first", "last" };
        var bar = list.Select(x => new Foo {
            Name = x,
            Id = Guid.NewGuid()
        }); //.ToList();
        Console.WriteLine("bar Count = " + bar.Count());
        Console.WriteLine();
        for (int i = 0; i < 3; i++) { 
            Console.WriteLine(bar.First().Name + " " + bar.First().Id); …
Run Code Online (Sandbox Code Playgroud)

c# linq list .net-core

0
推荐指数
1
解决办法
177
查看次数

标签 统计

.net-core ×1

c# ×1

linq ×1

list ×1