【发布时间】:2012-03-16 14:21:26
【问题描述】:
这两种工作方式都有效,但我想知道性能是否存在差异:
Dim collection As ItemCollection = CType(CellCollection.Where(Function(i) i.IsPending = True), ItemCollection)
For Each item As Item In collection
'Do something here
Next
和
For Each item As Item In CellCollection.Where(Function(i) i.IsPending = True)
'Do something here
Next
我认为第二个更好,因为你的变量更少,看起来更干净,但再想一想,我不太确定当你在迭代中放置 linq 查询时会发生什么。
是否必须在每次循环时重新评估?哪一个是最干净/性能最好的?
提前致谢。
【问题讨论】:
-
如果您对性能感兴趣 - 测试一下!
-
为什么要添加 '= True' 或者这在 VB 中是必需的吗?
-
我认为不同之处在于第一个版本不起作用,因为
Where()不会返回您的ItemCollection,而是IEnumerable(Of T)。 (假设这是正常的Enumerable.Where()并且ItemCollection上没有重载的强制转换运算符。)
标签: vb.net performance linq iteration