我有以下函数
Public Function GetPositionBySchoolID(ByVal SchoolID As Integer) As CPAPositionsDataTable
Dim positions As New CPAPositionsDataTable
Dim tmp = (From pos In PositionsAdapter.GetData()
Where (pos.SchoolID = SchoolID)
Select pos)
tmp.ToList.ForEach(Sub(i) positions.ImportRow(i))
Return positions
End Function
我想知道是否有任何方法可以减少将LINQ结果分配给tmp并直接处理结果,即
Public Function GetPositionBySchoolID(ByVal SchoolID As Integer) As CPAPositionsDataTable
Dim positions As New CPAPositionsDataTable
(From pos In PositionsAdapter.GetData()
Where (pos.SchoolID = SchoolID)
Select pos).ToList.ForEach(Sub(i) positions.ImportRow(i))
Return positions
End Function
可以,但不能使用查询语法:
PositionsAdapter.GetData().Where(Function(pos) pos.SchoolID = SchoolID) _
.ToList().ForEach(Sub(i) positions.ImportRow(i))
您可以简单地在foreach循环中遍历它。LINQ操作符的返回值是IEnumerable<T>
,所以是可迭代的。
此外,您可以创建一个ForEach
方法(这不包括作为LINQ堆栈是关于不有副作用,和ForEach
都是关于副作用)作为IEnumerable<T>
的扩展,你不需要调用ToList
之前。