我的 code是:
using System;
using System.Linq;
using System.Collections.Generic;
public class D {
public string Value { get; set; }
public D(string v) { this.Value = v; }
}
public class Program
{
public static void Main()
{
List<D> DD = new List<D>();
DD.Add(new D("2018-11-08"));
DD.Add(new D("2018-12-01"));
var dd = DD.Where(d=> {
Console.WriteLine($"In predicate: {d.Value}");
return d.Value=="2018-12-01";
});
var dl = dd.ToList();
foreach (var d in dl) {
Console.WriteLine($"Final: {d.Value}");
}
}
}
输出是:
In predicate: 2018-11-08
In predicate: 2018-12-01
Final: 2018-12-01
如果DD
包含100万个元素,DD.Where
中的lambda会执行100万次吗?
如果 DD
是来自 SQL Server 的表怎么办?
最佳答案
If DD contains 1 million elements, will the lambda in DD.Where execute 1 million times?
答案取决于急切操作。在这种特定情况下是,因为急切的操作是ToList
,但说它是First()
,Any()
一旦找到符合条件的元素,它就会短路。
关于c# - Where 方法如何运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53206922/