返回集合集合的语法是什么?

时间:2018-09-19 15:17:41

标签: vb.net linq

下面的代码显示了我如何尝试返回对象集合的集合。使用基于lambda的LINQ语法,我得到了想要的东西。我不使用“自然语言”语法;我得到了一组平面对象。获取收藏集的用语是什么?

Option Strict On

Sub Main

    Dim i As Integer
    Dim j As Integer
    Dim l As List(Of List(Of MyPoint)) = New List(Of List(Of MyPoint))
    For j = 1 To 2
        Dim ll = New List(Of MyPoint)
        For i = 1 To 10
            ll.Add(New MyPoint(i,j))
        Next
        l.Add(ll)
    Next

    Dim q1 = l.Select(Function (ll) ll.Select(Function (p) New MyPoint(p.x+1, p.y+1)))

    Dim q2 = From ll In l From p In ll Select New MyPoint(p.x + 1, p.y + 1)



End Sub

Public Class MyPoint
    Public Property X As Integer
    Public Property Y As Integer
    Sub New(i As Integer, j As Integer)
        X = i
        Y = j
    End Sub
End Class

编辑:使用真实的类而不是匿名类型,并使用Option Strict On,因此您可以专注于实际的问题,而不必跟我争论。

1 个答案:

答案 0 :(得分:2)

第一个中有两个Select。第二个还需要两个Select。 (我重命名了一些东西,以便于阅读)

Dim i As Integer, j As Integer
Dim listOfLists As List(Of List(Of Point)) = New List(Of List(Of Point))
For j = 1 To 2
    Dim ll = New List(Of Point)
    For i = 1 To 10
        ll.Add(New Point With {.x = i, .y = j})
    Next
    listOfLists.Add(ll)
Next

' This returns what I want    
Dim q1 = listOfLists.Select(Function(ll) ll.Select(Function(p) New With {.x = p.x + 1, .y = p.y + 1}))

Dim q2 = From thisList In listOfLists 
          Select From pnt In thisList 
                 Select New Point With {.x = pnt.x + 1, .y = pnt.y + 1}