HEADING

Listing 2. This abbreviated verson of the CreateXmlFromJoins procedure's code from the sample project's VbLinqDemo form joins the dbNwind.Orders entity with XDocuments opened from XML document files created by the GenerateAndTestNwindXmlFiles procedure. Joins to Employees and Shippers XDocuments are omitted for brevity. Join code in Where clauses is emphasized by bold font for clarity.

Private Sub CreateXmlFromJoins()
  Dim strCountry As String = "Switzerland"
  'Load all the XML documents except Orders.xml
  Dim Custs = XDocument.Load(strPath + "Customers.xml")
  Dim Items = XDocument.Load(strPath + "LineItems.xml")
  Dim Empls = XDocument.Load(strPath + "Employees.xml")
  Dim Prods = XDocument.Load(strPath + "Products.xml")
  Dim Supps = XDocument.Load(strPath + "Suppliers.xml")
  Dim Ships = XDocument.Load(strPath + "Shippers.xml")

  Dim docSalesOrders = _
  <?xml version="1.0" standalone="yes"?>
  <SalesOrders>
    <%= Select _
      <SalesOrder EmployeeID=<%= o.EmployeeID %>
        CustomerID=<%= o.CustomerID %>
        ShipVia=<%= o.ShipVia %>>
        <OrderID><%= o.OrderID %></OrderID>
        <%= Select _
          <BillTo CustomerID=<%= c.CustomerID(0).Value %>>
          <CompanyName>
              <%= c.CompanyName(0).Value %>
            </CompanyName>
            <Address><%= c.Address(0).Value %></Address>
            <City><%= c.City(0).Value %></City>
            <Region><%= c.Region(0).Value %></Region>
            <PostalCode>
              <%= c.PostalCode(0).Value %>
            </PostalCode>
            <Country><%= c.Country(0).Value %></Country>
          </BillTo> _
          From c In Custs.Root.Customer _
          Where c.CustomerID(0).Value = o.CustomerID %>
          <%= Select _
          <CustomerContact>
            <CustomerID>
              <%= c.CustomerID(0).Value %>
            </CustomerID>
            <ContactName>
              <%= c.ContactName(0).Value %>
            </ContactName>
            <ContactTitle>
              <%= c.ContactTitle(0).Value %>
            </ContactTitle>
            <Phone><%= c.Phone(0).Value %></Phone>
            <Fax><%= c.Fax(0).Value %></Fax>
          </CustomerContact> _
          From c In Custs.Root.Customer _
          Where c.CustomerID(0).Value = o.CustomerID %>
        <OrderDate><%= o.OrderDate %></OrderDate>
        <RequiredDate><%= o.RequiredDate %></RequiredDate>
        <ShipTo>
          <ShipName><%= o.ShipName %></ShipName>
          <ShipAddress><%= o.ShipAddress %></ShipAddress>
          <ShipCity><%= o.ShipCity %></ShipCity>
          <ShipRegion><%= o.ShipRegion %></ShipRegion>
          <ShipPostalCode>
            <%= o.ShipPostalCode %>
          </ShipPostalCode>
          <ShipCountry><%= o.ShipCountry %></ShipCountry>
        </ShipTo>
        <LineItems>
          <%= Select _
            <LineItem OrderID=<%= i.OrderID(0).Value %>>
              <Quantity><%= i.Quantity(0).Value %></Quantity>
              <%= Select _
                <Product>
                  <ProductID><%= p.ProductID(0).Value %></ProductID>
                  <ProductName>
                    <%= p.ProductName(0).Value %>
                  </ProductName>
                  <Package>
                    <%= p.QuantityPerUnit(0).Value %>
                  </Package>
                  <%= Select _
                    <Brand>
                      <BrandID><%= s.SupplierID(0).Value %></BrandID>
                      <Brand><%= s.CompanyName(0).Value %></Brand>
                      <Origin><%= s.Country(0).Value %></Origin>
                    </Brand> _
                    From s In Supps.Root.Supplier _
                    Where s.SupplierID(0).Value = _
                      p.SupplierID(0).Value %>
                </Product> _
                From p In Prods.Root.Product _
                Where p.ProductID(0).Value = _
                i.ProductID(0).Value %>
              <UnitPrice>
                <%= Format(CDec(i.UnitPrice(0).Value), "C2") %>
              </UnitPrice>
              <Discount>
                <%= Format(CDec(i.Discount(0).Value), "P1") %>
              </Discount>
              <Extended>
                <%= Format(i.Quantity(0).Value * i.UnitPrice(0).Value * _
                  (1 - i.Discount(0).Value), "C2") %>
              </Extended>
           </LineItem> _
            From i In Items.Root.LineItem _
            Where i.OrderID(0).Value = o.OrderID.ToString() _
          %>
        </LineItems>
         …
        %>
      </SalesOrder> _
      From o In dbNwind.Orders _
      Where o.ShipCountry = strCountry _
    %>
  </SalesOrders>
End Sub