我正在努力弄清楚如何在以下场景中删除并添加observableCollection
:
Dim flightsColl As New ObservableCollection(Of Flight)
Dim f1 As Flight = New Flight With {.FlightName = "Jet1"}
Dim f2 As Flight = New Flight With {.FlightName = "Boeing"}
Dim p1 As Passenger = New Passenger() With {.Name = "Tim", .Address = "address1"}
Dim p2 As Passenger = New Passenger() With {.Name = "Tom", .Address = "address2"}
Dim p3 As Passenger = New Passenger() With {.Name = "Tas", .Address = "address3"}
'add 2 passangers to Jet1
f1.Passengers.Add(p1)
f1.Passengers.Add(p2)
'add passnger3 to boeing
f2.Passengers.Add(p3)
'add flight1 & 2 to flight list
flightsColl.Add(f1)
flightsColl.Add(f2)
在上面例如,我有一个flightColl,这是一个可观察的集合 你可以看到它有2个航班,第一个有2个乘客,第二个有1个 乘客
现在,如果我必须将乘客2(p2)移动到2号航班,那么
来自flightsColl
我必须
Remove Passenger 2 from flight 1
Add Passenger 2 to flight 2
更新
Public Class Flight
Public Property Name() As String
Public Property Passengers As ICollection(Of Passenger)
Public Sub New()
Me.Appliances = New ObservableCollection(Of Passenger)
End Sub
结束班
答案 0 :(得分:0)
你为什么要通过flightColl?您可以参考航班和乘客。
f1.Passengers.Remove(p2)
...等
**更新**
Dim oldflight As Flight = flightsColl.FirstOrDefault(Function(f) f.FlightName = "oldFlightName")
Dim passenger As Passenger = oldflight.Passengers.FirstOrDefault(Function(p) p.Name = "passengerName")
oldflight.Passengers.Remove(passenger)
Dim newflight As Flight = flightsColl.FirstOrDefault(Function(f) f.FlightName = "newFlightName")
newflight.Passengers.Add(passenger)
这有帮助吗?您需要在那里添加一些错误检查(如果您不想意外删除具有相同名称的其他乘客,SingleOrDefault会更好)