[英]Find and Insert into list class collection?
我有一个叫做GroupSelect的类,并制作了一个List(Of GroupSelect)()
集合。
现在,我需要找到GroupSelect
与RowNo
和GroupNo
在List(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
在协作中找到该对象
grpSelect.RowNo = rowNo
grpSelect.GroupNo = grpNo
grpSelect.Value = CType(CType(row.FindControl("txtCombine"),
TextBox).Text, Integer)
这个怎么做?
假设收集您的List(Of GroupSelect)
和RowNo
, GroupNo
是你正在检查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
包围查询可能是一个好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.