title: Are there alternatives to deriving classes from LINQ to SQL joins and [Table] mappings?
body: <p>Pardon the length, please. Also, this is a completely hypothetical, off-the-cuff situation and code samples should not be scrutinized for complete accuracy.  </p>
  
  <p>So, .Net plays nice when you have a simple, straightforward LINQ:</p>
  
  <pre><code>Invoice tbl_invoice = from invoice in DbContext.Invoice 
                        where invoice.RecordID == 1 
                        select invoice;
  </code></pre>
  
  <p>Assuming there is an Invoice class as follows:</p>
  
  <pre><code>[Table]
  public class Invoice
  {
      [Column]
      public int RecordID {get; set;}
  
      [Column]
      public DateTime RecordDate {get; set;}
  }
  </code></pre>
  
  <p>Given the execution of the sample LINQ, the above class would be instanced with the appropriate values in the attributed Column properties.  </p>
  
  <p>Let's advance the query a bit:</p>
  
  <pre><code>var purchases = (from invoice in DbContext.Invoice 
                   join order in DbContext.Order on invoice.RecordID equals order.InvoiceID 
                   select new ProductPurchase 
                   { 
                       invoice.RecordID, 
                       invoice.RecordDate, 
                       order.PartID, 
                       order.Quantity 
                   })
                  .ToList();    // this is used to create a BindingList&lt;ProductPurchase&gt;
  </code></pre>
  
  <p>There must be two classes that resemble the following:</p>
  
  <pre><code>[Table]
  public class Order
  {
      [Column]
      public int InvoiceID {get; set;}
  
      [Column]
      public string OrderID {get; set;}
  
      [Column]
      public string Quantity {get; set;}
  }
  
  [Table]
  public class ProductPurchase : Invoice
  {
      [Column]
      public int InvoiceID {get; set;}
  
      [Column]
      public string PartID {get; set;}
  
      [Column]
      public string Quantity {get; set;}
  }
  </code></pre>
  
  <p>The goal in the second sample is to create a <code>BindingList&lt;T&gt;</code> so that the data can be used as a DataSource. So with the second LINQ sample we could do the following:</p>
  
  <pre><code>BindingList&lt;ProductPurchase&gt; purchases = new BindingList&lt;ProductPurchase&gt;(purchases);
  </code></pre>
  
  <p>We cannot do this as easily as it seems. We must inherit concrete classes in order for this to work. In my opinion, this is highly inelegant. I thought, <em>it sure would be nice if I could create an interface.</em> Something like:</p>
  
  <pre><code>[Table]
  public interface IInvoice
  {
      [Column]
      public int RecordID {get; set;}
  
      [Column]
      public DateTime RecordDate {get; set;}
  }
  </code></pre>
  
  <p>Now, assume there is a similar IOrder interface, I could create a ProductPurchase concrete class as follows:</p>
  
  <pre><code>[Table]
  public class ProductPurchase : IInvoice, IOrder
  {  }
  </code></pre>
  
  <p>...<strong>IF</strong> the <code>[TableAttribute()]</code> were allowed on interfaces.  </p>
  
  <p>Let's say I want to include a <code>join</code> to the PartID in tblParts that would retrieve a PartsDescription and PartsCost; only one base (concrete or abstract) class can be inherited. It seems like you have to create a bunch of junk derivations to be able to accommodate the joins. Is there an alternative? I grow weary of using DataTable/DataView as the data source to a DataGridView. It would be much simpler to be able to use the classes I've created as mappings.  </p>
  
  <p>One example of a task I am trying to complete:</p>
  
  <ol>
  <li>List records in DataGridView
  displaying certain joined data.</li>
  <li>User double-clicks the selected datagrid row view.</li>
  <li>New WinForm displays queried info now from several joined tables</li>
  </ol>
  
  <p>In step 2, when the user selects an Invoice, I convert the DataGridRowView to a DataRow. Not so bad, really. But when I finish the query, I have an anonymous datatype which can be used to instance a new DataTable and populate the values of the DataRow.  </p>
  
  <p>Binding the DataRow to the new WinForm is not as straightforward as binding a class. Thus my question and curiosity about alternatives. Any ideas would be helpful.</p>
  
  <p><strong>EDIT:</strong>
  A couple of links that explain a little more:</p>
  
  <ul>
  <li><a href="http://www.codeproject.com/KB/linq/AnonymousTypeTransform.aspx" rel="nofollow">Code Project</a></li>
  <li><a href="http://devlicio.us/blogs/derik_whittaker/archive/2008/04/25/linq2sql-explicit-construction-of-entity-exception.aspx" rel="nofollow">Linq2Sql - Explicit Construction of Entity Exception</a></li>
  </ul>
  
  <p><strong>EDIT 2:</strong><br>
  Let's say I have a query from 3 tables:</p>
  
  <p><strong>tblInvoices</strong>:<br>
          InvoiceID | InvoiceDate | Balance | Paid | PaidDate  </p>
  
  <p><strong>tblPurchaseItem</strong>:<br>
          PartNumber | InvoiceID | PartCost| PartDescription</p>
  
  <p><strong>tblInventory</strong>:
          PartNumber | OnHands | ReOrder | Restock</p>
  
  <p>Assume there are 3 classes decorated with [Table] mapping attribute with all appropriate [Column] mapping attributes { get; set; }.  </p>
  
  <p>I may have a method resembling the following:</p>
  
  <pre><code>public static BindingList&lt;InvoiceDetails&gt; GetInvoiceDetails (int InvoiceID)
  {
      // LINQ is probably incorrect - feel free to post corrections :)
      IList&lt;InvoiceDetails&gt; invDetails = (from purchItems in DbContext.PurchaseItemsMap 
                                          join invoices in DbContext.Invoices 
                                              on purchItems.InvoiceID equals invoices.InvoiceID
                                          join invItems in DbContext.InventoryItemsMap
                                              on purchItems.PartNumber equals invItems.PartNumber
                                          where purchItems.InvoiceID == InvoiceID
                                          where invItems.Restock == true
                                          select new InvoiceDetails
                                          {
                                              invoices.InvoiceID,
                                              purchItems.PartDescription,
                                              purchItems.PartCost,
                                              invItems.OnHands
                                          })
                                          .ToList();
  
      return new BindingList(invDetails);
  }
  </code></pre>
  
  <p>In order for this to work I would need the following:</p>
  
  <pre><code>[Table]
  public class InvoiceDetails : PurchaseItems
  {  // invoices column getters &amp; setters  }
  
  [Table]
  public class PurchaseItems : InventoryItems
  {  // purchasItems column getters &amp; setters  }
  
  [Table]
  public class InventoryItems
  {  // inventoryItems column getters &amp; setters  }
  </code></pre>
  
  <p>Now, which is more nonsensical? Having to create weird combinations of class derivations that would create a maintenance nightmare; or, hypothetically, create interfaces for table mappings and simply derive classes like InvoiceDetails as follows:</p>
  
  <pre><code>public class InvoiceDetails : IInventoryItems, IPurchaseItems, IInvoices
  {    }
  </code></pre>