Using an indexed SelectMany clause : Select « LINQ « VB.Net

Using an indexed SelectMany clause


Imports System.IO
Imports System.Reflection
Imports System.Linq
Imports System.Xml.Linq
Imports System.Collections
Imports System.Collections.Generic
Public Class Project
    Public ProjectID As Integer
    Public ProjectName As String
    Public Category As String
    Public Cost As Decimal
    Public YearLength As Integer
End Class

Public Class Employee
    Public EmployeeID As String
    Public CompanyName As String
    Public Address As String
    Public City As String
    Public Region As String
    Public PostalCode As String
    Public Country As String
    Public Phone As String
    Public Fax As String
    Public Payments As Payment()
End Class

Public Class Payment
    Public PaymentID As Integer
    Public PaymentDate As DateTime
    Public Total As Decimal
End Class

Public Class MainClass
    Public Shared Sub Main()
        Dim projectList As List(Of Project) = New List(Of Project)
        projectList.Add(New Project With {.ProjectID = 1, .ProjectName = "A", .Category = "Design", .Cost = 18D, .YearLength = 39})
        projectList.Add(New Project With {.ProjectID = 2, .ProjectName = "B", .Category = "Testing", .Cost = 19D, .YearLength = 17})
        projectList.Add(New Project With {.ProjectID = 3, .ProjectName = "C", .Category = "Coding", .Cost = 10D, .YearLength = 13})
        projectList.Add(New Project With {.ProjectID = 4, .ProjectName = "D", .Category = "Meeting", .Cost = 22D, .YearLength = 53})
        projectList.Add(New Project With {.ProjectID = 5, .ProjectName = "E", .Category = "Writing", .Cost = 21.35D, .YearLength = 0})
        projectList.Add(New Project With {.ProjectID = 6, .ProjectName = "F", .Category = "Testing", .Cost = 25D, .YearLength = 120})
        projectList.Add(New Project With {.ProjectID = 7, .ProjectName = "G", .Category = "Coding", .Cost = 30D, .YearLength = 15})
        projectList.Add(New Project With {.ProjectID = 8, .ProjectName = "H", .Category = "Design", .Cost = 40D, .YearLength = 6})
        projectList.Add(New Project With {.ProjectID = 9, .ProjectName = "I", .Category = "Coding", .Cost = 97D, .YearLength = 29})

        Dim employeeList As List(Of Employee) = New List(Of Employee)

        employeeList.Add(New Employee With {.EmployeeID = 1, .CompanyName = "Company A", .Address = "Street 1", .City = "City 1", .Region = "North"})
        employeeList.Add(New Employee With {.EmployeeID = 2, .CompanyName = "Company B", .Address = "Street 2", .City = "City 2", .Region = "South"})
        employeeList.Add(New Employee With {.EmployeeID = 3, .CompanyName = "Company C", .Address = "Street 3", .City = "City 3", .Region = "West"})
        employeeList.Add(New Employee With {.EmployeeID = 4, .CompanyName = "Company D", .Address = "Street 4", .City = "City 4", .Region = "East"})
        employeeList.Add(New Employee With {.EmployeeID = 5, .CompanyName = "Company E", .Address = "Street 5", .City = "City 5", .Region = "North"})
        employeeList.Add(New Employee With {.EmployeeID = 6, .CompanyName = "Company F", .Address = "Street 6", .City = "City 6", .Region = "South"})

        Dim employeePayments = employeeList.SelectMany(Function(cust, custIndex) _
                                                    From ord In cust.Payments _
                                                    Select "Employee #" & (custIndex + 1) & _
                                                           " has an order with PaymentID " & ord.PaymentID)

        For Each emp In employeePayments

    End Sub

End Class


Related examples in the same category

1.Add vbCrLf to the selected value
2.Renaming property in Select
3.Using Select to return a sequence of project names
4.Query Date with Linq
5.Query Reuse with deferred execution
6.Using Concat to create one sequence that contains each array's values
7.Using Concat to create one sequence
8.Using linq to query the Registry
9.Shows all keys under HKLM\Software that start with the letter C
10.Shows shortcuts to everything under My Recent Documents
11.Counts how many items are in your Favorites folder
12.Uses reflection to show all System assemblies which are currently loaded
13.Projecting Select
14.Using Select to produce a sequence of Integers one value higher
15.Transformation Select
16.Using Select to produce a sequence of strings from a sequence of Integers
17.Anonymous Types in Select Clause
18.Using Select to produce a sequence of each word in an array with new case.
19.Using 'Select New With' to create new objects
20.Using Select to produce a sequence of new objects from two arrays with calculation
21.Using an indexed Select clause to determine if the value of Integers match their position
22.Combine Select and Where to get value from an array based on the value from another array
23.Check for the second letter from each word in an array
24.Use a loop to print out each value in Linq query result
25.num is called the range variable which will take turns representing each value in the array.
26.Select with Function