' Visual Basic 2008 9.0 .NET Examples - LINQ - Language Integrated Query - New in Visual Basic 2008 - 9.0 : Option Infer On Option Strict On Public Class Person Private m_Name As String Public Property Name() As String Get Return m_Name End Get Set(ByVal value As String) m_Name = value End Set End Property Private m_City As String Public Property City() As String Get Return m_City End Get Set(ByVal value As String) m_City = value End Set End Property Private m_IsMale As Boolean Public Property IsMale() As Boolean Get Return m_IsMale End Get Set(ByVal value As Boolean) m_IsMale = value End Set End Property Public Overrides Function ToString() As String ToString = "Female " If IsMale Then ToString = "Male " ToString &= Name & ", from " & City & "." End Function End Class Public Class Example1 Public Shared Sub Main() Dim persons As Person() = New Person() { _ New Person With {.Name = "John", .City = "New York", .IsMale = True}, _ New Person With {.Name = "Bo", .City = "New York", .IsMale = False}, _ New Person With {.Name = "Jane", .City = "London", .IsMale = False}, _ New Person With {.Name = "Paul", .City = "New York", .IsMale = True}} ' Dim personsNY As IEnumerable(Of Person) = _ persons.Where(Function(person) person.City = "New York") _ .OrderBy(Function(person) person.Name) _ .Select(Function(person) person) ' For Each person As Person In personsNY Console.WriteLine(person) Next ' Console.ReadLine() End Sub End Class Public Class Example2 Public Shared Sub Main() Dim persons As Person() = New Person() { _ New Person With {.Name = "John", .City = "New York", .IsMale = True}, _ New Person With {.Name = "Bo", .City = "New York", .IsMale = False}, _ New Person With {.Name = "Jane", .City = "London", .IsMale = False}, _ New Person With {.Name = "Paul", .City = "New York", .IsMale = True}} ' Dim personsNY As IEnumerable(Of Person) = _ From person In persons _ Where person.City = "New York" _ Order By person.Name _ Select person ' For Each person As Person In personsNY Console.WriteLine(person) Next ' Console.ReadLine() End Sub End Class Public Class Example3 Public Shared Sub Main() Dim persons = New Person() { _ New Person With {.Name = "John", .City = "New York", .IsMale = True}, _ New Person With {.Name = "Bo", .City = "New York", .IsMale = False}, _ New Person With {.Name = "Jane", .City = "London", .IsMale = False}, _ New Person With {.Name = "Paul", .City = "New York", .IsMale = True}} ' Dim personNamesAndCitiesNY = From person In persons _ Where person.City = "New York" _ Order By person.Name _ Select person.Name, person.City ' For Each personNameAndCity In personNamesAndCitiesNY Console.WriteLine(personNameAndCity.Name & " from " & _ personNameAndCity.City) Next ' Console.ReadLine() End Sub End Class ' Visit www.studyvb.com for more examples. Copyright 2003-2008 De Wolf.