Vb.Net LINQ查询嵌套列表

时间:2014-05-24 15:43:03

标签: vb.net linq

我有cPoint对象列表。每个cPoint都有cPosition对象列表。我必须进行查询,只返回其cPosition符合条件的cPoints,并且每个cPoint必须只包含符合条件的那些cPosition。是否可以使用LINQ?

'classes
Public Class cPoint
    Private _PointName As String
    Public Positions As colPositions
    '...
End Class

Public Class cPosition
    Private _X As Double
    Private _Y As Double
    Private _Z As Double
    Private _IntervalStart As Long
    '...
End Class

Public Class cPoints
    Inherits System.ComponentModel.BindingList(Of cPoint)
End Class

Public Class cPositions
    Inherits System.ComponentModel.BindingList(Of cPosition)
End Class

我最接近的是这个查询,但它没有返回过滤的cPoints。查询中的每个项目都有两个对象:1。过滤的Point(带有未过滤的子位置)和2.过滤的属于该Point的位置。

Dim PtList As cPoints
'...
'my query 
Dim query = (From pt In PtList
             From pos In pt.Positions
             Where (pos.IntervalStart < 222)
             Select New With {Key .PT = pt,
             Key .PTPOS = (From position In pt.Positions Where (position.IntervalStart < 222)).ToList}).Distinct

1 个答案:

答案 0 :(得分:0)

所以你想要的位置ALL的间隔超过222?只需使用All Linq方法,如下例所示:

Module Module1
'classes
Public Class cPoint
    Private _PointName As String
    Public Positions As cPositions

    ''' <summary>
    ''' Needed because we want to construct cPoints object using cPosition list within Linq
    ''' </summary>
    ''' <param name="posList"></param>
    ''' <remarks></remarks>
    Public Sub New(posList As IEnumerable(Of cPosition))
        Positions = New cPositions
        For Each cp As cPosition In posList
            Me.Positions.Add(cp)
        Next
    End Sub

    'default
    Public Sub New()

    End Sub
End Class

Public Class cPosition
    Private _X As Double
    Private _Y As Double
    Private _Z As Double
    Private _IntervalStart As Long

    Public Sub New(x As Double, y As Double, z As Double, iss As long)
        _X = x
        _Y = y
        _Z = z
        _IntervalStart = iss
    End Sub


    ''' <summary>
    ''' Added a property to access from outside class
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property IntervalStart As Long
        Get
            Return _IntervalStart
        End Get
        Set(value As Long)
            _IntervalStart = value
        End Set
    End Property
End Class

Public Class cPoints
    Inherits System.ComponentModel.BindingList(Of cPoint)
End Class

Public Class cPositions
    Inherits System.ComponentModel.BindingList(Of cPosition)
End Class

Sub Main()

    Dim ptlist As cPoints = New cPoints
    Dim point1 = New cPoint
    point1.Positions = New cPositions
    point1.Positions.Add(New cPosition(1, 2, 3, 4))
    point1.Positions.Add(New cPosition(1, 2, 3, 1000))
    point1.Positions.Add(New cPosition(1, 2, 3, 2000))
    point1.Positions.Add(New cPosition(1, 2, 3, 1))

    Dim point2 = New cPoint
    point2.Positions = New cPositions
    point2.Positions.Add(New cPosition(1, 2, 3, 1))
    point2.Positions.Add(New cPosition(1, 2, 3, 2))
    point2.Positions.Add(New cPosition(1, 2, 3, 444))
    point2.Positions.Add(New cPosition(1, 2, 3, 555))

    ptlist.Add(point1)
    ptlist.Add(point2)

    'selects all the points in ptlist that have all their positions with interval start more than 222.
    Dim result = From pt In ptlist
                 Where pt.Positions.All(Function(l As cPosition)
                                            'put whatever condition here.
                                            Return l.IntervalStart > 222
                                        End Function)
                Select pt

    'select all the points. each point, will have all the positions that are less than 222.
    Dim result2 = From pt In ptlist
                  Select New cPoint(pt.Positions.Where(Function(l As cPosition)
                                                           Return l.IntervalStart < 222
                                                       End Function))

    For Each a In result2
        Console.WriteLine(a.Positions.Count)
    Next


End Sub

End Module