本文整理汇总了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}")
示例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}")
输出:
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}")
输出:
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}")
示例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}")
输出:
First array: passionfruit Second array: No single item found