wolfeh wolfeh - 5 months ago 24
Vb.net Question

object Collection Containing a List of IDs Linq

I have a list box and the user is able to multi-select. I want to use Linq and bring back the records of the selected IDs that the user selects. I need to bring back the full object record for each selected ID

Here is the contact object along with collection object

Namespace MODEL
<System.Serializable()> _
Public Class ContactCollection
Inherits System.Collections.ObjectModel.Collection(Of Contact)
Implements IList(Of Contact)
End Class
End Namespace


Namespace MODEL
<System.Serializable()> _
Public Class Contact

Private mContactID As Int32 = 0
Private mFirstName As String
Private mLastName As String

Public Property ContactID As Int32
Get
Return mContactID
End Get
Set(value As Int32)
mContactID = value
End Set
End Property

Public Property FirstName As String
Get
Return mFirstName
End Get
Set(value As String)
mFirstName = value
End Set
End Property

Public Property LastName As String
Get
Return mLastName
End Get
Set(value As String)
mLastName = value
End Set
End Property

End Class
End Namespace


Adding 5 Records to the collection object

Dim objCollection As New MODEL.ContactCollection
Dim obj As New MODEL.Contact

objCollection.Add(New MODEL.Contact With {
.ContactID = 1, _
.FirstName = "John", _
.LastName = "Smtih" _
})

objCollection.Add(New MODEL.Contact With {
.ContactID = 2, _
.FirstName = "Mark", _
.LastName = "Davis" _
})

objCollection.Add(New MODEL.Contact With {
.ContactID = 3, _
.FirstName = "Tom", _
.LastName = "Howe" _
})

objCollection.Add(New MODEL.Contact With {
.ContactID = 4, _
.FirstName = "Jerry", _
.LastName = "Thomas" _
})

objCollection.Add(New MODEL.Contact With {
.ContactID = 5, _
.FirstName = "Jane", _
.LastName = "Marry" _
})


This is the selected contact List from the list box

Dim lstContacts As New List(Of Integer)
lstContacts.Add(2)
lstContacts.Add(4)


I am not sure what to do at this point with Linq to find the values. I think I have to use contains but I have tried may different ways but I was unable to get the values.

I have tried this Linq but does not work or bring any records back

Dim objSearch from SearchContacts in objCollection
Where (lstContacts.Contains(SearchContacts.ContactID))

Answer

To get the Ids, try that :

Dim ids As IEnumerable(Of Int32) = myListBox.SelectedItems _
                                    .OfType(Of Contact)() _
                                    .Select( Function(c) c.ContactID ) _

Edit

If you want the Contacts, you can just just :

Dim ids As IEnumerable(Of Contact) = myListBox.SelectedItems _
                                    .OfType(Of Contact)()

And if you want the contacts in a separate copied collection, you can :

Dim ids As List(Of Contact) = myListBox.SelectedItems _
                                    .OfType(Of Contact)() _
                                    .ToList()

Last (if think this is your real question - just tell and I erase everything above)

Dim selectedContacts As IEnumerable(Of MODEL.Contact) = From contact In objCollection
                                                           Join id In lstContacts
                                                           On contact.ContactID Equals id
                                                           Select contact
Comments