[英]Find and Insert into list class collection?
I have class called GroupSelect and made a collection List(Of GroupSelect)()
. 我有一个叫做GroupSelect的类,并制作了一个
List(Of GroupSelect)()
集合。
Now I need to find GroupSelect
with RowNo
and GroupNo
in List(Of GroupSelect)()
. 现在,我需要找到
GroupSelect
与RowNo
和GroupNo
在List(Of GroupSelect)()
If I found at same index update Value
. 如果我发现相同的索引,请更新
Value
。 If not found add to List(Of GroupSelect)
. 如果找不到,则添加到
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
Find this Object in the colleaction 在协作中找到该对象
grpSelect.RowNo = rowNo
grpSelect.GroupNo = grpNo
grpSelect.Value = CType(CType(row.FindControl("txtCombine"),
TextBox).Text, Integer)
How to do this? 这个怎么做?
Assuming collection is your List(Of GroupSelect)
and RowNo
, GroupNo
are the values you are checking againt and Value
is the new value 假设收集您的
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()
should be ideal in your case because it doesn't look like you will never have more than one item matching the criteria. SingleOrDefault()
在您的情况下应该是理想的,因为它看起来像您永远不会有多个符合条件的项目。 But it might be a good idea to surround the query with with Try..Catch
. 但是用
Try..Catch
包围查询可能是一个好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.