title: Using reflection to address a Linqed property
body: <p>I'm trying to writing a generic method that will load a record of a specific type, with a specific ID.  Here's one way that works:</p>
  
  <pre><code>    public abstract class LinqedTable&lt;T&gt; where T : LinqableTable {
      public static T Get(long ID) {
          DataContext context = LinqUtils.GetDataContext&lt;T&gt;();
          var q = from obj in context.GetTable&lt;T&gt;()
                          where obj.ID == ID
                          select obj;
          return q.Single&lt;T&gt;();
      }
  }
  
  public abstract class LinqableTable {
      public abstract long ID { get; set; }
  }
  </code></pre>
  
  <p>You can ignore the call to <code>LinqUtils.GetDataContext&lt;T&gt;()</code>; that's a utility function I've got to deal with the fact that I have multiple data contexts in my program.  The point is that now I can declare any of my classes as subclasses of <code>LinqableTable</code>, and I can easily instantiate a record of that table just by calling <code>LinqedTable&lt;MyType&gt;.Get(ID)</code>.</p>
  
  <p>This has some limitations, however.  Firstly, it forces all of my tables to have an I
  identity field of type <code>long</code>, named <code>ID</code>.  Secondly, because I'm using an abstract method, I am forced to go to the O/R designer and change the inheritance property of every ID field in my system to "override".  </p>
  
  <p>I want more flexibility than that.  So naturally, I tried reflection, and came out with the following:</p>
  
  <pre><code>    public abstract class LinqedTable&lt;T&gt; where T : LinqableTable {
      public static T Get(long ID) {
          DataContext context = LinqUtils.GetDataContext&lt;T&gt;();
          var q = from obj in context.GetTable&lt;T&gt;()
                          where obj.IDValue == ID
                          select obj;
          return q.Single&lt;T&gt;();
      }
  }
  
  public abstract class LinqableTable {
      internal long IDValue {
          get { return (long)IDProperty.GetValue(this, null); }
          set { IDProperty.SetValue(this, value, null); }
      }
      internal PropertyInfo IDProperty {
          get { return this.GetType().GetProperty(IDPropertyName); }
      }
      internal protected virtual string IDPropertyName {
          get { return "ID"; }
      }
  }
  </code></pre>
  
  <p>Theoretically, this allows me to override the ID column name, the cast to <code>long</code> should be OK with any integral data type, and I don't need to go defining all my ID columns as <code>overrides</code>.</p>
  
  <p><strong>BUT</strong></p>
  
  <p>Linq doesn't like this.  On the call to <code>q.Single&lt;T&gt;();</code> I get a runtime error:</p>
  
  <p><code>The member 'EISS.Utils.LinqableTable.IDValue' has no supported translation to SQL.</code></p>
  
  <p>OK, today I learned that Linq does some kind of magic on the back end; it doesn't instantiate <code>obj</code> and just read the <code>IDValue</code> property.  So must be there's some attribute that needs to be set on the <code>IDValue</code> property that lets Linq do its thing.</p>
  
  <p>But what?</p>