例如,我从循环111 222 333获得3个数据 然后我想返回如下值:
'111', '222', '333'
所以,如何结合并完成与substring (')
和(,)
的循环。
因为我执行条件为where in ('111','222','333')
的查询。
这是我在vb.net中的代码:
For Each grv As GridViewRow In gridDisplayGRN.Rows
Dim chk As CheckBox = CType(grv.FindControl("ChkSelect"), CheckBox)
If chk.Checked = True Then
Dim p As New PengirimanDetail
p.KodePenerimaan = grv.Cells(2).Text.Trim()
Dim parm = p.KodePenerimaan => first looping get 111 and next looping 222 333
Dim data = "'" + parm + "'" => '111'
Dim return = '111','222','333' ???
End If
Next
请给我真实的代码,谢谢......
答案 0 :(得分:0)
有几种方法可以解决这个问题,我将向您展示其中的一个。
在循环之后,您将所有收集的字符串连接到所需的格式,此处通过String.Join。
Dim parms as new List(Of String) 'collection for KodePenerimaans
For Each grv As GridViewRow In gridDisplayGRN.Rows
Dim chk As CheckBox = CType(grv.FindControl("ChkSelect"), CheckBox)
If chk.Checked = True Then
Dim p As New PengirimanDetail
p.KodePenerimaan = grv.Cells(2).Text.Trim()
parms.Add(String.Concat("'", p.KodePenerimaan, "'")) 'Add current KodePenerimaan to list
End If
Next
Dim result as String = String.Empty
If parms.Count > 0 Then
result = String.Join(",", parms) 'Join to "'111','222','333'"
End If
return result