开发者

find item in list of class?

I have class called GroupSelect and made a collection List(Of GroupS开发者_如何学编程elect)().

Now I need find to RowNo = 4 in List(Of GroupSelect)() and get GroupSelect object.

Public Class GroupSelect
Public Property RowNo() As Integer
    Get
        Return m_RowNo
    End Get
    Set(ByVal value As Integer)
        m_RowNo = value
    End Set
End Property
Private m_RowNo As Integer
Public Property GroupNo() As Integer
    Get
        Return m_GroupNo
    End Get
    Set(ByVal value As Integer)
        m_GroupNo = value
    End Set
End Property
Private m_GroupNo As Integer

End Class

How can I do that?


Make sure you are using System.Linq and then it's as easy as:

list.FirstOrDefault(Function(item) item.RowNo = 4)

Or if you aren't familiar with the predicate syntax

(From item In list Where item.RowNo = 4 Select Item).FirstOrDefault())

EDIT: Changed to VB syntax, which is notepad-compiled :-)


You can do it in Linq with less code, but here is a more n00bish / old fashioned way of doing it if you don't want to mess Linq...

Private Function GetRowNumberFour(ByVal lstYourList as List(of GroupSelect)) As GroupSelect

   For each obj as GroupSelect In lstYourList

      If obj.RowNo = 4 Then

         Return obj

      End If

   Next

End Function

This function will return the GroupSelect object that you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜