Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET LINQ-集合中的集合计数?_Vb.net_Linq - Fatal编程技术网

VB.NET LINQ-集合中的集合计数?

VB.NET LINQ-集合中的集合计数?,vb.net,linq,Vb.net,Linq,以以下场景为例: Public Class Store Public Overridable Property Areas As List(Of Area) End Class Public Class Area Public Overridable Property Shelves As List(Of Shelf) End Class Public Class Shelf Public Property ID as integer End Class 获取商店货

以以下场景为例:

Public Class Store
    Public Overridable Property Areas As List(Of Area)
End Class

Public Class Area
    Public Overridable Property Shelves As List(Of Shelf)
End Class

Public Class Shelf
    Public Property ID as integer
End Class
获取商店货架总数的最快方法是什么? 也就是说,对于一家商店,我通过使用面积来获得总面积。计数

或者我需要在每个区域循环并统计货架数量吗?

在C#中:

转换为VB:

Dim count = Store.Areas.SelectMany(Function(x) x.Shelves).Count()

(使用,而不是VB guy)

使用此LINQ表达式

Dim count As Integer = store.Areas.Sum(Function(a) a.Shelves.Count())

请注意,这与@BrokenGlass”的答案不同。他首先使用
SelectMany
将嵌套集合展平,然后计算结果项的总数,即循环计算项目总数。其中,as I仅在外部集合上循环,并对内部集合的
Count
属性求和。这应该快得多。

很棒的东西,正是我想要的。感谢您的快速回复!
Dim count As Integer = store.Areas.Sum(Function(a) a.Shelves.Count())