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


VB.NET Queryable.FirstOrDefault方法代码示例

本文整理汇总了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)
开发者ID:VB.NET开发者,项目名称:System.Linq,代码行数:7,代码来源:Queryable.FirstOrDefault

输出:

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))
开发者ID:VB.NET开发者,项目名称:System.Linq,代码行数:12,代码来源:Queryable.FirstOrDefault

输出:

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")))
开发者ID:VB.NET开发者,项目名称:System.Linq,代码行数:21,代码来源:Queryable.FirstOrDefault

输出:

The first long name is 'Andersen, Henriette Thaulow'.
There is NOT a name that is longer than 30 characters.


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