title: How to display data from related tables in a WPF datagrid
body: <p>I'm just finding my way with WPF/Datagrid/Linq and I'm trying to find out how to display data from a related table in my datagrid.  For example I have 3 tables: </p>
  
  <ul>
  <li>Customers: id, name</li>
  <li>Products: id, name</li>
  <li>Orders: id, customerId, productId</li>
  </ul>
  
  <p>and I've set up the relations in the DB tables.  I've added the tables as a C# DataSet which has created my wrapper classes.  I now want to bind a query to a couple of different WPF datagrids to obtain the following: </p>
  
  <ul>
  <li>list of orders as | Order Id | Product name | Customer Name |</li>
  <li>list of customers as | Customer Name | Summary of orders (eg comma separated list of strings) |</li>
  </ul>
  
  <p>How can I specify the DataContext or ItemsSource in my XAML to display this information from other tables?  I'm using <code>ObjectDataProvider</code>s to provide design-time support, if that makes any difference.</p>
  
  <p>I've seen this question: <a href="https://stackoverflow.com/questions/1863621/how-to-bind-wpf-datagrid-to-a-joined-table">How to bind WPF Datagrid to a joined table</a> which creates a new class to display the information for the table. Is this how I need to do it rather than specifying a binding?</p>
  
  <p>EDIT: </p>
  
  <p>I've had a bit of success using the 2nd method above.  I wonder though whether I can use bindings to achieve the same thing without needing to do an explicit <code>select</code> query to create the new items.  I've got a query that I map to the datagrid: </p>
  
  <pre><code>var q = from d in dbMan.dataset.Orders
     select new { 
       id=d.id, 
       productName = d.ProductsRow.name, 
       custName = d.CustomersRow.name };
  </code></pre>
  
  <p>and then bind in the XAML: </p>
  
  <pre><code>&lt;my:DataGrid.Columns&gt;
    &lt;my:DataGridTextColumn Header="id" Binding="{Binding id}" /&gt;
    &lt;my:DataGridTextColumn Header="Product Name" Binding="{Binding productName}" /&gt;
    &lt;my:DataGridTextColumn Header="Customer Name" Binding="{Binding custName}" /&gt;
  &lt;/my:DataGrid.Columns&gt;
  </code></pre>