使用 For Each 将集合分配给集合数据项

Using For Each to assign collection to collection data item

在 Blue Prisms vb.net 环境中工作,目前正在构建 LDAP VBO 实用程序。我有以下代码,它将 return 广告组中的用户列表,即 samAccountNamegivennamesurname

我使用的命名空间导入是:

System
System.Drawing, System.Data
System.DirectoryServices
System.Collections.Generic

我的问题是尝试遍历结果集并将每一行数据存储 returned 在我的最终收集数据项中,colResults

我知道我正在处理一个集合,我知道我在 For Each 循环中所做的事情不正确,即编译错误:

"String cannot be converted to System.Data.Datatable"

但是我无法解决。

我试图重写并使用索引,但在语法上我不能将 .Count 与我的 Properties.SearchResultCollection.

一起使用
Dim de As New DirectoryServices.DirectoryEntry("LDAP://" + Path)
Dim ds As New DirectoryServices.DirectorySearcher(de)
Dim srCol As DirectoryServices.SearchResultCollection
Dim sr As DirectoryServices.SearchResult

'Try and assume success
Success = True

Try

    ds.Filter = "(&(objectCategory=user)(memberOf=" + GroupDN + "))"
    ds.PropertiesToLoad.Add("givenname")
    ds.PropertiesToLoad.Add("sn")
    ds.PropertiesToLoad.Add("samAccountName")

    srCol = ds.FindAll()

    For Each sr In srCol
        If Not sr.GetDirectoryEntry.Properties("name").Value Is Nothing Then
            colResults = (sr.GetDirectoryEntry.Properties("givenname").Value.ToString())
            colResults = (sr.GetDirectoryEntry.Properties("sn").Value.ToString())
            colResults = (sr.GetDirectoryEntry.Properties("samAccountName").Value.ToString)
        End If
    Next
Catch e As System.Exception
    Success = False
End Try

当从 Blue Prism 中的代码阶段返回 DataTable 时,代码阶段不知道输出参数的列是什么——您通常会在代码中为其分配一个新的 DataTable,定义列,然后填充行。

因此,您应该在代码阶段的开头初始化 colResults DataTable:

colResults = new DataTable
' make sure the output parameter has these columns defined (or none at all)
colResults.Columns.Add("givenname")
colResults.Columns.Add("sn")
colResults.Columns.Add("samAccountName")

并继续创建数据行并将它们从循环内添加到 table:

For ...
    If ...
        Dim newRow as DataRow = colResults.NewRow
        newRow("givenname") = ...
        newRow("sn") = ...
        newRow("samAccountName") = ...
        colResults.Rows.Add(newRow)
    End If
Next

DataRow classes' documentation 也显示了一个 well-detailed 示例。