我在vb.net开发
Dim dtDetails As DataTable = Utility.GetDetailsTable()
Dim resource_id As String = ""
Dim dtrow = dtDetails.Select("id='" & user_id & "'")
If dtrow.Length > 0 Then
For i As Integer = 0 To dtrow.Length - 1
If i > 0 Then
resource_id += ","
End If
resource_id += Convert.ToString(dtrow(i)("instance_id"))
Next
但是当我进一步使用resource_id时,它不起作用,Docs返回为空
Dim resource_ids = New BsonValue() {resource_id}
Dim querys = Query.And(
Query.EQ("user_id", user_id),
Query.In("resource_id", resource_ids)
)
Dim Docs = ceilometer.GetCollection("meter").Find(querys)
因为我需要这种格式BsonValue(){"resource_id1","resource_id2"}
。请帮我解决这个问题
答案 0 :(得分:1)
从我理解你的问题的方式来看,我认为你需要将动态ID添加到BsonValue
数组中。使用此answer的扩展程序:
<Extension()> _
Public Sub Add(Of T)(ByRef arr As T(), item As T)
Array.Resize(arr, arr.Length + 1)
arr(arr.Length - 1) = item
End Sub
然后您可以在代码中使用:
Dim dtDetails As DataTable = Utility.GetDetailsTable()
Dim resource_ids As new BsonArray
Dim dtrow = dtDetails.Select("id='" & user_id & "'")
If dtrow.Length > 0 Then
For i As Integer = 0 To dtrow.Length - 1
resource_ids.Add(Convert.ToString(dtrow(i)("instance_id")))
Next
End If
Dim queries = Query.And(
Query.EQ("user_id", user_id),
Query.In("resource_id", resource_ids)
)
Dim Docs = ceilometer.GetCollection("meter").Find(queries)