Determine which people have a non-empty Pet array : Any « LINQ « VB.Net






Determine which people have a non-empty Pet array

 

Imports System
Imports System.Linq
Imports System.Collections.Generic
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Structure Person
    Public LastName As String
    Public Pets() As Pet
End Structure

Public Class Example

    Public Shared Sub Main() 
            Dim people As New List(Of Person)(New Person() _
                {New Person With {.LastName = "A", .Pets = New Pet() {New Pet With {.Name = "A1", .Age = 10}, _
                                                                      New Pet With {.Name = "A2", .Age = 14}, _
                                                                      New Pet With {.Name = "A3", .Age = 6}}}, _
                  New Person With {.LastName = "B", .Pets = New Pet() {New Pet With {.Name = "B1", .Age = 1}}}, _
                  New Person With {.LastName = "C", .Pets = New Pet() {New Pet With {.Name = "C1", .Age = 8}}}, _
                  New Person With {.LastName = "D", .Pets = New Pet() {New Pet With {.Name = "D1", .Age = 2}, _
                                                      New Pet With {.Name = "D2", .Age = 13}}}})
    
        Dim names = From person In people Where person.Pets.Any() Select person.LastName
    
        For Each name As String In names
            Console.WriteLine(name)
        Next
    
    End Sub
End Class

   
  








Related examples in the same category

1.Any Example
2.Using Any to return a grouped a list of projectList only for categories