作者:VB.NET开发
项目:System.Lin
Dim numbers As New List(Of Integer)(New Integer() {1, 2})
' Determine if the list contains any elements.
Dim hasElements As Boolean = numbers.AsQueryable().Any()
MsgBox(String.Format("The list {0} empty.", _
IIf(hasElements, "is not", "is")))
作者:VB.NET开发
项目:System.Lin
Structure Pet
Public Name As String
Public Age As Integer
End Structure
Structure Person
Public LastName As String
Public Pets() As Pet
End Structure
Sub AnyEx2()
Dim people As New List(Of Person)(New Person() _
{New Person With {.LastName = "Haas", _
.Pets = New Pet() {New Pet With {.Name = "Barley", .Age = 10}, _
New Pet With {.Name = "Boots", .Age = 14}, _
New Pet With {.Name = "Whiskers", .Age = 6}}}, _
New Person With {.LastName = "Fakhouri", _
.Pets = New Pet() {New Pet With {.Name = "Snowball", .Age = 1}}}, _
New Person With {.LastName = "Antebi", _
.Pets = New Pet() {}}, _
New Person With {.LastName = "Philips", _
.Pets = New Pet() {New Pet With {.Name = "Sweetie", .Age = 2}, _
New Pet With {.Name = "Rover", .Age = 13}}}})
' Determine which people have a non-empty Pet array.
Dim names = From person In people _
Where person.Pets.AsQueryable().Any() _
Select person.LastName
For Each name As String In names
Console.WriteLine(name)
Next
' This code produces the following output:
'
' Haas
' Fakhouri
' Philips
End Sub
作者:VB.NET开发
项目:System.Lin
Structure Pet
Dim Name As String
Dim Age As Integer
Dim Vaccinated As Boolean
End Structure
Shared Sub AnyEx3()
' Create an array of Pet objects.
Dim pets() As Pet = _
{New Pet With {.Name = "Barley", .Age = 8, .Vaccinated = True}, _
New Pet With {.Name = "Boots", .Age = 4, .Vaccinated = False}, _
New Pet With {.Name = "Whiskers", .Age = 1, .Vaccinated = False}}
' Determine whether any pets over age 1 are also unvaccinated.
Dim unvaccinated As Boolean = _
pets.AsQueryable().Any(Function(p) p.Age > 1 And p.Vaccinated = False)
MsgBox(String.Format( _
"There {0} unvaccinated animals over age one.", _
IIf(unvaccinated, "are", "are not any") _
))
End Sub