简体   繁体   English

FindAll和Select有什么区别?

[英]What's the difference between FindAll and Select?

I used List.Select(condition).Count() and found the result to be inappropriate and then I tried List.FindAll(condition).Count() and it worked fine. 我使用List.Select(condition).Count()并发现结果不合适然后我尝试了List.FindAll(condition).Count()并且它工作正常。

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

You see different results because 你看到不同的结果,因为

list.Select(condition)

transforms the list into a sequence of True and False values with the length that is always equal to the number of items in the list . 将列表转换为TrueFalse值的序列,其长度始终等于list的项目数。 If you use Where instead of Select , you will get matching results. 如果您使用Where而不是Select ,您将获得匹配的结果。

However, a shorter way to get the result is to pass the condition to Count , like this: 但是,获得结果的更简单方法是将条件传递给Count ,如下所示:

var res = list.Count(condition);

List.Select Invokes a transform function on each element in the sequence and returns the transformed collection. List.Select对序列中的每个元素调用转换函数并返回转换后的集合。 In general, using this will return the same Count as the original collection. 通常,使用此选项将返回与原始集合相同的Count

List.FindAll takes a predicate (similar to List.Where ) and so will only return elements matching it, giving a different count from the original. List.FindAll采用谓词(类似于List.Where ),因此只会返回与其匹配的元素,从而提供与原始元素不同的计数。

Your code or way to filter out list is fundamentally wrong. 您的代码或过滤列表的方法根本就是错误的。 What you are trying to do is: 你要做的是:

 list.Select(mylist => mylist.key == "somevalue").Count();

This will return the same count as length of List. 这将返回与List的长度相同的计数。 The resulting collection will be true/false for each item. 生成的集合对于每个项目都是真/假。

You need Enumerable.Where like: 你需要Enumerable.Where喜欢:

 list.Select(mylist => mylist.key == "somevalue").Count();

This will give you the same count as you are finding in FindAll method. 这将为您提供与在FindAll方法中找到的计数相同的计数。

You can even pass the predicate to Count like: 您甚至可以将谓词传递给Count,如:

var count = list.Count(mylist => mylist.Key == "somevalue");

FindAll is different from Enumerable.Where in a way that FindAll constructs a new list, whereas Enumerable.Where returns an IEnumerable<T> . FindAllEnumerable.Where不同之处在于FindAll构造一个新列表,而Enumerable.Where返回一个IEnumerable<T> You can read this question for more info: C# FindAll VS Where Speed 您可以阅读此问题以获取更多信息: C#FindAll VS Where Speed

Using Select performs a mapping operation, not a filter operation. 使用Select执行映射操作,而不是过滤操作。 This means you'll always get the same number of items out of the query as you had in the original list. 这意味着您将始终从查询中获得与原始列表中相同数量的项目。 In your case, you projected the list from a List<T> to an IEnumerable<bool> (with the original number of true or false responses). 在您的情况下,您将列表从List<T>投影到IEnumerable<bool> (具有原始的真或假响应数)。

Use .Where to filter a list. 使用.Where.Where过滤列表。

This line: 这一行:

List.select(mylist => mylist.key == "somevalue")

creates a list of values "true" and "false" but does not filter the output. 创建值“true”和“false”的值列表,但不过滤输出。 Therefore the count is the same as without select. 因此,计数与没有选择相同。

List.select(mylist => mylist.key == "somevalue").count() 

is the same as 是相同的

List.count();

And

List.where(mylist => mylist.key == "somevalue").count()

works the way you want. 以你想要的方式工作。

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

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