Lessons in this course (12)

10. LINQ, XML literals, async and iterators

IntermediateMediumLesson 10 of 12
vb
' LINQ query syntax
Dim expensive = From p In products
                Where p.Price > 1000
                Order By p.Price Descending
                Select p.Name, p.Price

' Aggregate and Group By
Dim byCategory = From p In products
                 Group By p.Category Into Total = Sum(p.Price), Count()

' XML literals — unique to VB: XML directly in code, with embedded expressions
Dim doc = <orders>
              <%= From o In orders Select <order id=<%= o.Id %> total=<%= o.Total %>/> %>
          </orders>
Dim ids = From x In doc...<order> Select CInt(x.@id)    ' ... = descendants, .@ = attribute

' Async / Await
Public Async Function GetPageAsync(url As String) As Task(Of String)
    Using client As New HttpClient()
        Return Await client.GetStringAsync(url)
    End Using
End Function

' Iterator functions = C# yield return
Public Iterator Function Evens(max As Integer) As IEnumerable(Of Integer)
    For i = 0 To max Step 2
        Yield i
    Next
End Function