當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。