Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查找并插入到列表类集合中?_C#_Vb.net - Fatal编程技术网

C# 查找并插入到列表类集合中?

C# 查找并插入到列表类集合中?,c#,vb.net,C#,Vb.net,我有一个名为GroupSelect的类,并创建了一个集合列表(GroupSelect)() 现在我需要使用RowNo和GroupNo在列表(GroupSelect的)中查找GroupSelect。如果我在同一个索引中找到更新的值。如果未找到,请添加到列表(组选择) 在操作中查找此对象 grpSelect.RowNo = rowNo grpSelect.GroupNo = grpNo grpSelect

我有一个名为GroupSelect的类,并创建了一个集合
列表(GroupSelect)(

现在我需要使用
RowNo
GroupNo
列表(GroupSelect的)中查找
GroupSelect
。如果我在同一个索引中找到更新的
。如果未找到,请添加到
列表(组选择)

在操作中查找此对象

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

如何执行此操作?

假设集合是您的
列表(组选择)
行号
组号
是您要再次检查的值,
是新值

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
可能是个好主意

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