本文整理汇总了VB.NET中System.Linq.Queryable.SingleOrDefault方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Queryable.SingleOrDefault方法的具体用法?VB.NET Queryable.SingleOrDefault怎么用?VB.NET Queryable.SingleOrDefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了Queryable.SingleOrDefault方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: fruits1
' Create two arrays. The second is empty.
Dim fruits1() As String = {"orange"}
Dim fruits2() As String = {}
' Get the only item in the first array, or else
' the default value for type string (null).
Dim fruit1 As String = fruits1.AsQueryable().SingleOrDefault()
MsgBox("First Query: " + fruit1)
' Get the only item in the second array, or else
' the default value for type string (null).
Dim fruit2 As String = fruits2.AsQueryable().SingleOrDefault()
MsgBox("Second Query: " & _
IIf(String.IsNullOrEmpty(fruit2), "No such string!", fruit2))
输出:
First Query: orange Second Query: No such string!
示例2: pageNumbers
Dim pageNumbers() As Integer = {}
' Setting the default value to 1 after the query.
Dim pageNumber1 As Integer = pageNumbers.AsQueryable().SingleOrDefault()
If pageNumber1 = 0 Then
pageNumber1 = 1
End If
MsgBox(String.Format("The value of the pageNumber1 variable is {0}", pageNumber1))
' Setting the default value to 1 by using DefaultIfEmpty() in the query.
Dim pageNumber2 As Integer = pageNumbers.AsQueryable().DefaultIfEmpty(1).Single()
MsgBox(String.Format("The value of the pageNumber2 variable is {0}", pageNumber2))
输出:
The value of the pageNumber1 variable is 1 The value of the pageNumber2 variable is 1
示例3: fruits
Dim fruits() As String = _
{"apple", "banana", "mango", "orange", "passionfruit", "grape"}
' Get the single string in the array whose length is greater
' than 10, or else the default value for type string (null).
Dim fruit1 As String = _
fruits.AsQueryable().SingleOrDefault(Function(fruit) fruit.Length > 10)
' Display the result.
MsgBox("First Query: " & fruit1)
' Get the single string in the array whose length is greater
' than 15, or else the default value for type string (null).
Dim fruit2 As String = _
fruits.AsQueryable().SingleOrDefault(Function(fruit) fruit.Length > 15)
MsgBox("Second Query: " & _
IIf(String.IsNullOrEmpty(fruit2), "No such string!", fruit2))
输出:
First Query: passionfruit Second Query: No such string!