FindAll 和 Select 有什么区别?

Dot*_*son 3 .net c# linq

我使用List.Select(condition).Count()并发现结果不合适,然后我尝试了List.FindAll(condition).Count()它,效果很好。

List.Select(condition).count 如何工作?

das*_*ght 5

你会看到不同的结果,因为

list.Select(condition)
Run Code Online (Sandbox Code Playgroud)

将列表转换为True和False值的序列,其长度始终等于 中的项数list。如果使用Where而不是Select,您将获得匹配结果。

但是,获得结果的更短方法是将条件传递给Count,如下所示:

var res = list.Count(condition);
Run Code Online (Sandbox Code Playgroud)