Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I create a facade for identical LINQ 2 SQL tables?
    text
    copied!<p>I'm working with a 14-year old schema where a parent table (P) contains a type (8 possibilities) and location (3 possibilities) field that together point to one of twenty-four specific tables (A1, A2, A3, B1, B2, B3...), all with identical schemas for their type.</p> <p>I would like to create a generic way to access the specific tables but cannot seem to make it work.</p> <p>Let's say the parent table is called Document and two types of specific are (NewLetter, CurrentLetter, and HistoricLetters) and (NewBill, CurrentBill, and HistoricBills).</p> <pre><code> public interface ILetter { DateTime CreationDate { get; } } public interface IBill { DateTime BillDate { get; } } // The LINQ 2 SQL generated classes already implement the CreationDate property public partial class NewLetter : ILetter { } public partial class CurrentLetter : ILetter { } public partial class HistoricLetter : ILetter { } </code></pre> <p>Then in my code, I would like to be able to do this:</p> <pre><code> switch (type) { case 1: Table&lt;IBill&gt; specificBill; switch (location) { ... } case 2: Table&lt;ILetter&gt; specificTable; switch (location) { case 1: specificTable = dataContext.NewLetter as Table&lt;ILetter&gt;; break; case 2: specificTable = dataContext.CurrentLetter ... case 3: specificTable = dataContext.HistoricLetter ... } } </code></pre> <p>Or something similar. Unfortunately I can't cast <code>Table&lt;NewLetter&gt;</code> to <code>Table&lt;ILetter&gt;</code> even if NewLetter implements ILetter. Is there any way around this? I guess I'm basically trying to create a view in C#, as I don't have the permissions to create one in the db itself.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload