如何执行 LINQ 语句而不将其分配给变量?

发布于 2024-11-01 19:36:24 字数 808 浏览 0 评论 0原文

我有以下函数

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲琵琶半遮面シ 2024-11-08 19:36:24

是的,可以,但不能使用查询语法:

PositionsAdapter.GetData().Where(Function(pos) pos.SchoolID = SchoolID) _
                          .ToList().ForEach(Sub(i) positions.ImportRow(i))

Yes you can, but you can't use the query syntax:

PositionsAdapter.GetData().Where(Function(pos) pos.SchoolID = SchoolID) _
                          .ToList().ForEach(Sub(i) positions.ImportRow(i))
゛清羽墨安 2024-11-08 19:36:24

您可以简单地在 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, and ForEach is all about side effects) as an extension on IEnumerable<T>, you don't have to call ToList before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文