Namespace IntroToLINQ
Public Class Cars
Public PetName As String = String.Empty
Public Color As String = String.Empty
Public Speed As Integer
Public Make As String = String.Empty
Public Overrides Function ToString() As String
Return String.Format("Make={0}, Color={1}, Speed={2}, PetName={3}", Make, Color, Speed, PetName)
End Function
End Class
Public Class MainProgram
Public Shared Sub Main()
Console.WriteLine("***** Fun with Query Expressions *****")
'this array will be th basis of our testing
Dim myCars As Cars() = { _
New Cars With {.PetName = "Henry", .Color = "Silver", .Speed = 100, .Make = "BMW"}, _
New Cars With {.PetName = "Daisy", .Color = "Black", .Speed = 55, .Make = "VW"}, _
New Cars With {.PetName = "Mary", .Color = "Tan", .Speed = 90, .Make = "BMW"}, _
New Cars With {.PetName = "Clunker", .Color = "Rust", .Speed = 15, .Make = "Yugo"}, _
New Cars With {.PetName = "Hank", .Color = "Red", .Speed = 65, .Make = "Ford"}, _
New Cars With {.PetName = "Sven", .Color = "White", .Speed = 55, .Make = "VW"}, _
New Cars With {.PetName = "Mary", .Color = "Yellow", .Speed = 80, .Make = "VW"}, _
New Cars With {.PetName = "Zippy", .Color = "Green", .Speed = 75, .Make = "Ford"}, _
New Cars With {.PetName = "Melvin", .Color = "White", .Speed = 55, .Make = "Ford"}, _
New Cars With {.PetName = "Calvin", .Color = "Tan", .Speed = 65, .Make = "VW"} _
}
BasicSelections(myCars)
GetSubsets(myCars)
GetProjection(myCars)
ReversedSelection(myCars)
OrderedResults(myCars)
GetDiff()
Console.ReadLine()
End Sub
Shared Sub BasicSelections(ByVal myCars As Cars())
'get all cars. Similar to Select * in SQL
Console.WriteLine(vbLf & "All Cars: ")
Dim allCars = From c In myCars Select c
For Each c In allCars
Console.WriteLine("Car: {0}", c)
Next
'get only th pet names
Console.WriteLine(vbLf & "All PetNames:")
Dim allNames = From c In myCars Select c.PetName
For Each n In allNames
Console.WriteLine("Pet Name: {0}", n)
Next
'allNames is really a variable that implements IEnumerable(Of String) given that we are selecting only
'the values of the PetName property for each
'car object
Dim makes = From c In myCars Select c.Make Distinct
For Each m In makes
Console.WriteLine("Make: {0}", m)
Next
End Sub
Shared Sub GetSubsets(ByVal myCars As Cars())
'now get only the BMWs
Console.WriteLine("Only BMWs:")
Dim onlyBMWs = From c In myCars Where c.Make = "BMW" Select c
For Each n In onlyBMWs
Console.WriteLine("Name: {0}", n)
Next
End Sub
'project new forms of data from an existing data source
Shared Sub GetProjection(ByVal myCars As Cars())
'now get structured data that only accounts for the
'make and color of each item
Console.WriteLine(vbLf & "Makes and Color:")
Dim makesColors = From c In myCars Select New With {c.Make, c.Color}
For Each n In makesColors
Console.WriteLine("Name: {0}", n)
Next
End Sub
Shared Sub ReversedSelection(ByVal myCars As Cars())
'get everything in reverse
Console.WriteLine("All cars in reverse:")
Dim subset = (From c In myCars Select c).Reverse()
For Each c As Cars In subset
Console.WriteLine("{0} is going {1} MPH", c.PetName, c.Speed)
Next
End Sub
Shared Sub OrderedResults(ByVal myCars As Cars())
' order all the cars by PetName
Dim subset = From c In myCars Order By c.PetName Select c
Console.WriteLine("Ordered by PetName:")
For Each c As Cars In subset
Console.WriteLine("Car {0}", c)
Next
'now find the cars that are going faster than 55 mph, and order by descending PetName
subset = From c In myCars Where c.Speed > 55 Order By c.PetName Descending Select c
Console.WriteLine(vbLf & "Cars going faster than 55, ordered by descending PetName:")
For Each c As Cars In subset
Console.WriteLine("Car {0}", c)
Next
End Sub
'the last LINQ query we will examine involves obtaining a resutl set the determines the differences
'between two iEnumberable(of T) compatible containers
Shared Sub GetDiff()
'two lists of strings
Dim myCars As String() = {"Yugo", "Aztec", "BMW"}
Dim yourCars As String() = {"BMW", "Saab", "Aztec"}
'find the differences
Dim carDiff = (From c In myCars Select c).Except(From c2 In yourCars Select c2)
Console.WriteLine(vbLf & "here is what you don't have, but I do:")
For Each s As String In carDiff
'prints yugo
Console.WriteLine(s)
Next
End Sub
End Class
End Namespace
Module Module1
Sub Main()
Console.WriteLine("***** LINQ Transformations *****" & vbLf)
Dim subset As IEnumerable(Of String) = GetStringSubset()
For Each item As String In subset
Console.WriteLine(item)
Next
Console.ReadLine()
End Sub
Function GetStringSubset() As IEnumerable(Of String)
Dim currentVideoGames() As String = {"Morrowind", "BioShock", "Half Life 2: Episode 1", "The Darkness", "Daxter", "System Shock 2"}
'note subset is an IEnumerable(OF String) compatible object
Dim subset As IEnumerable(Of String) = From g In currentVideoGames Where g.Length > 6 Order By g Select g
Return subset
End Function
End Module
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment