[英]linq, calculating the count of counts
I'm trying to figure out if I have a list of objects that have their own list of objects, how to get the total count. 我试图弄清楚是否有自己的对象列表的对象列表,以及如何获取总数。 For example:
例如:
public class FileWrapper {
List<File> Files
}
public class File {
...
}
So with a List wrappers, how do I get the total count of files. 因此,使用列表包装器,如何获取文件总数。
wrappers.ForEach(f => f.Files.Count).Count()
要Sum
每个列表的数量,不Count
的每个表的计数:
var sum = wrappers.Sum(file => file.Files.Count);
wrappers.Select(f => f.Files.Count).Sum()
You also can use SelectMany
. 您也可以使用
SelectMany
。
var sum = wrappers.SelectMany(wrapper => wrapper.Files).Count();
I do not have access to VS. 我没有使用VS的权限。 So it is eyes-compiled.
因此令人眼花eyes乱。
Like @Servy says, it is a bad idea : 就像@Servy所说的,这是一个坏主意:
This forces every single inner list to be iterated to determine its count, when they're capable of just returning the already-known Count.
当它们能够返回已知的Count时,这将强制迭代每个内部列表以确定其计数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.