2011-03-10 105 views
0

我有一个名为GroupSelect的类并做了一个集合List(Of GroupSelect)()查找并插入列表类集合?

现在我需要找到GroupSelectRowNoGroupNoList(Of GroupSelect)()。如果我发现在相同的索引更新Value。如果找不到,请加到List(Of GroupSelect)

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 

    Private m_Value As Integer 
    Public Property Value() As Integer 
     Get 
      Return m_Value 
     End Get 
     Set(ByVal value As Integer) 
      m_Value = value 
     End Set 
    End Property 

End Class 

查找此对象在colleaction

   grpSelect.RowNo = rowNo 
       grpSelect.GroupNo = grpNo 
       grpSelect.Value = CType(CType(row.FindControl("txtCombine"), 
TextBox).Text, Integer) 

如何做到这一点?

回答

0

假设收集您的List(Of GroupSelect)RowNoGroupNo是你正在检查againt和Value值是新值

Dim item As GroupSelect = collection.Where(Function(i) i.RowNo = RowNo AndAlso i.GroupNo = GroupNo).SingleOrDefault() 

If item Is Nothing Then 
    Dim newItem As New GroupSelect() 
    newItem.GroupNo = GroupNo 
    newItem.RowNo = RowNo 

    newItem.Value = Value 

    collection.Add(newItem) 
Else 
    item.Value = Value 
End If 

SingleOrDefault()应该在你的情况下,理想的,因为它看起来并不像你将永远不会有多个项目符合标准。但用Try..Catch环绕查询可能是个好主意。