本文整理汇总了VB.NET中System.Linq.Queryable.FirstOrDefault方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Queryable.FirstOrDefault方法的具体用法?VB.NET Queryable.FirstOrDefault怎么用?VB.NET Queryable.FirstOrDefault使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了Queryable.FirstOrDefault方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: numbers
' Create an empty array.
Dim numbers() As Integer = {}
' Get the first item in the array, or else the
' default value for type int, which is 0.
Dim first As Integer = numbers.AsQueryable().FirstOrDefault()
MsgBox(first)
输出:
0
示例2: List
Dim months As New List(Of Integer)(New Integer() {})
' Setting the default value to 1 after the query.
Dim firstMonth1 As Integer = months.AsQueryable().FirstOrDefault()
If firstMonth1 = 0 Then
firstMonth1 = 1
End If
MsgBox(String.Format("The value of the firstMonth1 variable is {0}", firstMonth1))
' Setting the default value to 1 by using DefaultIfEmpty() in the query.
Dim firstMonth2 As Integer = months.AsQueryable().DefaultIfEmpty(1).First()
MsgBox(String.Format("The value of the firstMonth2 variable is {0}", firstMonth2))
输出:
The value of the firstMonth1 variable is 1 The value of the firstMonth2 variable is 1
示例3: names
Dim names() As String = {"Hartono, Tommy", "Adams, Terry", _
"Andersen, Henriette Thaulow", _
"Hedlund, Magnus", "Ito, Shu"}
' Get the first string in the array that is longer
' than 20 characters, or the default value for type
' string (null) if none exists.
Dim firstLongName As String = _
names.AsQueryable().FirstOrDefault(Function(name) name.Length > 20)
MsgBox(String.Format("The first long name is '{0}'.", firstLongName))
' Get the first string in the array that is longer
' than 30 characters, or the default value for type
' string (null) if none exists.
Dim firstVeryLongName As String = _
names.AsQueryable().FirstOrDefault(Function(name) name.Length > 30)
MsgBox(String.Format( _
"There is {0} name that is longer than 30 characters.", _
IIf(String.IsNullOrEmpty(firstVeryLongName), "NOT a", "a")))
输出:
The first long name is 'Andersen, Henriette Thaulow'. There is NOT a name that is longer than 30 characters.