包含ID列表的对象集合Linq

时间:2016-06-30 15:18:02

标签: vb.net entity-framework linq linq-to-sql

我有一个列表框,用户可以多选。我想使用Linq并返回用户选择的所选ID的记录。我需要为每个选定的ID带回完整的对象记录

以下是联系人对象以及集合对象

 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

向集合对象添加5条记录

            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" _
            })

这是列表框中选定的联系人列表

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

我不知道在这一点上如何与Linq一起找到值。我想我必须使用包含,但我尝试过不同的方式,但我无法获得值。

我试过这个Linq但是没有工作或带回任何记录

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

1 个答案:

答案 0 :(得分:1)

要获得ID,请尝试:

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

修改

如果你想要联系人,你可以只是:

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

如果您想将联系人放在单独的复制集合中,您可以:

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

最后(如果认为这是你真正的问题 - 只需说出并删除上面的所有内容)

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