当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET Enumerable.SingleOrDefault方法代码示例

本文整理汇总了VB.NET中System.Linq.Enumerable.SingleOrDefault方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Enumerable.SingleOrDefault方法的具体用法?VB.NET Enumerable.SingleOrDefault怎么用?VB.NET Enumerable.SingleOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。


在下文中一共展示了Enumerable.SingleOrDefault方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。

示例1: fruits1

' Create an array that contains one item.
Dim fruits1() As String = {"orange"}

' Get the single item in the array or else a default value.
Dim result As String = fruits1.SingleOrDefault()

' Display the result.
Console.WriteLine($"First array: {result}")
开发者ID:VB.NET开发者,项目名称:System.Linq,代码行数:8,代码来源:Enumerable.SingleOrDefault

示例2: fruits2

' Create an empty array.
Dim fruits2() As String = {}

result = String.Empty

' Get the single item in the array or else a default value.
result = fruits2.SingleOrDefault()

' Display the result.
Dim output As String =
IIf(String.IsNullOrEmpty(result), "No single item found", result)
Console.WriteLine($"Second array: {output}")
开发者ID:VB.NET开发者,项目名称:System.Linq,代码行数:12,代码来源:Enumerable.SingleOrDefault

输出:

First array: orange
Second array: No single item found

示例3: pageNumbers

Dim pageNumbers() As Integer = {}

' Setting the default value to 1 after the query.
Dim pageNumber1 As Integer = pageNumbers.SingleOrDefault()
If pageNumber1 = 0 Then
    pageNumber1 = 1
End If
Console.WriteLine($"The value of the pageNumber1 variable is {pageNumber1}")

' Setting the default value to 1 by using DefaultIfEmpty() in the query.
Dim pageNumber2 As Integer = pageNumbers.DefaultIfEmpty(1).Single()
Console.WriteLine($"The value of the pageNumber2 variable is {pageNumber2}")
开发者ID:VB.NET开发者,项目名称:System.Linq,代码行数:12,代码来源:Enumerable.SingleOrDefault

输出:

The value of the pageNumber1 variable is 1
The value of the pageNumber2 variable is 1

示例4: fruits

' Create an array of strings.
Dim fruits() As String =
{"apple", "banana", "mango", "orange", "passionfruit", "grape"}

' Get the single item in the array whose length is > 10.
Dim fruit1 As String =
fruits.SingleOrDefault(Function(fruit) fruit.Length > 10)

' Display the result.
Console.WriteLine($"First array: {fruit1}")
开发者ID:VB.NET开发者,项目名称:System.Linq,代码行数:10,代码来源:Enumerable.SingleOrDefault

示例5:

' Get the single item in the array whose length is > 15.
Dim fruit2 As String =
fruits.SingleOrDefault(Function(fruit) fruit.Length > 15)

' Display the result.
Dim output As String =
IIf(String.IsNullOrEmpty(fruit2), "No single item found", fruit2)
Console.WriteLine($"Second array: {output}")
开发者ID:VB.NET开发者,项目名称:System.Linq,代码行数:8,代码来源:Enumerable.SingleOrDefault

输出:

First array: passionfruit
Second array: No single item found


注:本文中的System.Linq.Enumerable.SingleOrDefault方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。