如何执行 LINQ 语句而不将其分配给变量?
我有以下函数
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
I have the following function
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
And I was wondering if there is any way to cut out assigning the LINQ result to tmp and just work with the result directly, i.e.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,可以,但不能使用查询语法:
Yes you can, but you can't use the query syntax:
您可以简单地在 foreach 循环中迭代它。 LINQ 运算符的返回值是 IEnumerable深层的,因此可迭代。
另外,您可以创建一个
ForEach
方法(未包含该方法,因为 LINQ 堆栈不会有副作用,而ForEach
就是全部关于副作用)作为IEnumerable
的扩展,您之前不必调用ToList
。you can simply iterate over it in a foreach loop. The return value of the LINQ operators are
IEnumerable<T>
deep down, so iterateable.Also, you could create a
ForEach
method (this wasn't included as the LINQ stack is about not having side effects, andForEach
is all about side effects) as an extension onIEnumerable<T>
, you don't have to callToList
before.