studio-web
latest
false
重要 :
请注意,此内容已使用机器翻译进行了部分本地化。 新发布内容的本地化可能需要 1-2 周的时间才能完成。
UiPath logo, featuring letters U and I in white

Studio Web 用户指南

上次更新日期 2026年3月16日

实用的 VB 函数

VB 函数:Where

Where() 函数返回一个从零开始的数组,其中包含基于指定筛选条件的字符串数组的子集。

假设您将一个名为 words 的变量定义为 List(Of String) = {"apple", "banana", "cherry", "date"}

要获取包含字母“a”的单词列表,请应用 Where() 函数,如下所示:

words.Where(Function(w) w.Contains("a")).ToList()
words.Where(Function(w) w.Contains("a")).ToList()

输出为 {"apple", "banana", "date"}

VB 函数:Select

Select() 函数将创建一个新数组,其中包含将 Lambda 表达式应用于源数组中的每个元素的结果。

假设您将一个名为 numbers 的变量定义为 Integer() = {1, 2, 3, 4, 5}

要获取数字乘以自身的列表,请应用 Select() 函数,如下所示:

numbers.Select(Function(n) n * n).ToArray()
numbers.Select(Function(n) n * n).ToArray()

输出为 {1, 4, 9, 16, 25}

VB 函数:Aggregate

Aggregate()函数对数组中的所有元素执行计算并返回单个值。 此函数可用于聚合一列中的多个值。

假设您将一个名为 numbers 的变量定义为 Integer() = {1, 2, 3, 4, 5}

要获取数组中所有数字的总和,请应用 Aggregate() 函数,如下所示:

Dim sum = numbers.Aggregate(Function(acc, n) acc + n)
Dim sum = numbers.Aggregate(Function(acc, n) acc + n)

输出为 15

VB 函数:Group By

GroupBy() 函数通过键选取器函数对序列中的元素进行分组。

假设您将一个名为 words 的变量定义为 String() = {"apple", "banana", "cherry", "date"}

要按第一个字母(即键选取器函数)对单词进行分组,请应用 GroupBy() 函数,如下所示:

words.GroupBy(Function(w) w(0))
words.GroupBy(Function(w) w(0))

输出为 { {"a", "apple", "date"}, {"b", "banana"}, {"c", "cherry"} }

VB 函数:Order By

OrderBy()OrderByDescending() 函数根据键选取器函数对序列中的元素进行排序。

假设您将一个名为 words 的变量定义为 String() = {"apple", "banana", "cherry", "date"}

要按词长(即键选取器函数)对单词进行排序,请应用 OrderBy() 函数,如下所示:

words.OrderBy(Function(w) w.Length).ToArray()
words.OrderBy(Function(w) w.Length).ToArray()

输出为 {"date", "apple", "cherry", "banana"}

VB 函数:Join

Join() 函数根据键选取器函数合并两个序列中的元素。

假设您有两个变量:

  • names 定义为 String() = {"John", "Jane", "Joe"}
  • ages 定义为 Integer() = {25, 30, 35}

要将第一个序列中的元素与第二个序列中的元素合并,请应用 Join() 函数,如下所示:

names.Join(ages, Function(name) name(0), Function(age) age Mod 10, Function(name, age) $"{name}: {age}")
names.Join(ages, Function(name) name(0), Function(age) age Mod 10, Function(name, age) $"{name}: {age}")

输出为 {"John: 25", "Jane: 30", "Joe: 35"}

VB 函数:First

First() 函数返回序列中满足指定条件的第一个元素。

假设您将一个名为 numbers 的变量定义为 Integer() = {1, 2, 3, 4, 5}

要获取序列中的第一个偶数,请应用 First() 函数,如下所示:

numbers.First(Function(n) n Mod 2 = 0)
numbers.First(Function(n) n Mod 2 = 0)

输出为 2

VB 函数:First Or Default

FirstOrDefault() 函数返回第一个元素,如果没有元素满足条件,则返回默认值。

假设您将一个名为 numbers 的变量定义为 Integer() = {1, 2, 3, 4, 5}

要获取序列中的第一个奇数,请应用 FirstOrDefault() 函数,如下所示:

numbers.FirstOrDefault(Function(n) n Mod 2 = 1)
numbers.FirstOrDefault(Function(n) n Mod 2 = 1)

输出为 1

VB 函数:Last

Last() 函数返回序列中满足指定条件的最后一个元素。

假设您将一个名为 numbers 的变量定义为 Integer() = {1, 2, 3, 4, 5}

要获取序列中的第一个偶数,请应用 last() 函数,如下所示:

numbers.Last(Function(n) n Mod 2 = 0)
numbers.Last(Function(n) n Mod 2 = 0)

输出为 4

VB 函数:Last Or Default

LastOrDefault() 函数返回最后一个元素,如果没有元素满足条件,则返回默认值。

假设您将一个名为 numbers 的变量定义为 Integer() = {1, 2, 3, 4, 5}

要获取序列中的第一个奇数,请应用 FirstOrDefault() 函数,如下所示:

numbers.LastOrDefault(Function(n) n Mod 2 = 1)
numbers.LastOrDefault(Function(n) n Mod 2 = 1)

输出为 5

VB 函数:Skip

Skip() 函数会跳过序列中指定数量的元素。

假设您将一个名为 numbers 的变量定义为 Integer() = {1, 2, 3, 4, 5}

要跳过序列中的前两个元素,请应用 Skip() 函数,如下所示:

numbers.Skip(2).ToArray()
numbers.Skip(2).ToArray()

输出为 {3, 4, 5}

VB 函数:Skip While

SkipWhile() 函数会跳过元素,直到不再满足条件为止。

假设您将一个名为 numbers 的变量定义为 Integer() = {1, 2, 3, 4, 5}

要跳过小于 3 的数字,请应用 SkipWhile() 函数,如下所示:

numbers.SkipWhile(Function(n) n < 3).ToArray()
numbers.SkipWhile(Function(n) n < 3).ToArray()

输出为 {3, 4, 5}

VB 函数:Take

Take() 函数从序列开头返回指定数量的元素。

假设您将一个名为 numbers 的变量定义为 Integer() = {1, 2, 3, 4, 5}

要获取序列中的前三个元素,请应用 Take() 函数,如下所示:

numbers.Take(3).ToArray()
numbers.Take(3).ToArray()

输出为 {1, 2, 3}

VB 函数:Take While

TakeWhile() 会返回元素,直到不再满足条件为止。

假设您将一个名为 numbers 的变量定义为 Integer() = {1, 2, 3, 4, 5}

要获取小于 4 的数字,请应用 TakeWhile() 函数,如下所示:

numbers.TakeWhile(Function(n) n < 4).ToArray()
numbers.TakeWhile(Function(n) n < 4).ToArray()

输出为 {1, 2, 3}

VB 函数:Any

如果序列中的任何元素满足指定条件,则 Any() 函数将返回 true

假设您将一个名为 numbers 的变量定义为 Integer() = {1, 2, 3, 4, 5}

要确定序列中是否至少有一个数字为偶数,请应用 Any() 函数,如下所示:

numbers.Any(Function(n) n Mod 2 = 0)
numbers.Any(Function(n) n Mod 2 = 0)

输出为 true

VB 函数:All

如果序列中的所有元素都满足指定条件,则 All() 将返回 true

假设您将一个名为 numbers 的变量定义为 Integer() = {1, 2, 3, 4, 5}

要确定序列中的所有数字是否都是正数,请应用 All() 函数,如下所示:

numbers.All(Function(n) n > 0)
numbers.All(Function(n) n > 0)

输出为 true

VB 函数:Add item to list

列表函数 AddItemToList() 可将新项目添加到现有列表中。

要将项目添加到通用列表,请为其分配以下值:

AddItemToList(Processes.ALLDATATYPES.out_genericList, MainPage.EditGrid.NewItem)
AddItemToList(Processes.ALLDATATYPES.out_genericList, MainPage.EditGrid.NewItem)

VB 函数:Delete item from list

列表函数 DeleteItemFromList() 用于从现有列表中删除项目。

要从通用列表中删除项目,请为其分配以下值:

DeleteItemFromList(Processes.ALLDATATYPES.out_genericList, MainPage.EditGrid.RowIndex)
DeleteItemFromList(Processes.ALLDATATYPES.out_genericList, MainPage.EditGrid.RowIndex)

VB 函数:Update list item at index

列表函数 UpdateListItemAtIndex() 可更新现有列表中的项目。

要更新通用列表中的项目,请为其分配以下值:

UpdateListItemAtIndex(Processes.ALLDATATYPES.out_genericList, MainPage.EditGrid.RowIndex, MainPage.EditGrid.SelectedItem)
UpdateListItemAtIndex(Processes.ALLDATATYPES.out_genericList, MainPage.EditGrid.RowIndex, MainPage.EditGrid.SelectedItem)

此页面有帮助吗?

连接

需要帮助? 支持

想要了解详细内容? UiPath Academy

有问题? UiPath 论坛

保持更新