title: Get parent property from Expression function
body: <p>So let's say that I have the following classes:</p>
  
  <pre><code>public class Model {
      public AnotherModel InnerModel {
          get;
          set;
      }
  }
  
  public class AnotherModel {
      public String Value{
          get;
          set;
      }
  }
  </code></pre>
  
  <p>Now I have the following function:</p>
  
  <pre><code>public static void Foo&lt;T, U&gt;(Expression&lt;Func&lt;T, U&gt;&gt; func) {
       // Get the property info from func
  }
  </code></pre>
  
  <p>What I would like to do now is the following:</p>
  
  <pre><code>Foo&lt;Model, String&gt;(o =&gt; o.InnerModel.Value)
  </code></pre>
  
  <p>Here comes the problem:</p>
  
  <p>I know that you can fetch the PropertyInfo from the expression func by doing:</p>
  
  <pre><code>PropertyInfo propertyInfo = (PropertyInfo)((MemberExpression)func.Body).Member;
  </code></pre>
  
  <p>This will get me the PropertyInfo of the Value property. However, I would also like to get information about the parent property, that is the InnerModel property.</p>
  
  <p>What I know so far is that I can do the following:</p>
  
  <pre><code>((MemberExpression)func.Body).Expression
  </code></pre>
  
  <p>to fetch information of the parent property. However, it doesn't seem to be possible to extract a PropertyInfo from the Expression itself.</p>
  
  <p>Is there some way of actually retrieving the PropertyInfo of the Expression?</p>
  
  <p>Edit:
  To clarify, and it might be a bad way of attempting it but here goes:
  I can't use EntityFramework for this, just to make sure that that is understood.</p>
  
  <p>There is a database which I need to communicate with through an API.</p>
  
  <p>This database got the usual relations like the manner:
  Table Thread
       UserID -> Users.UserID</p>
  
  <p>This now extract to models. To follow the above example:</p>
  
  <pre><code>class Thread {
      [Reference(USER_USERID)]
      [TableKey(THREAD_USERID)]
      public User user {
          get;set;
      }
  }
  
  class User {
       [TableKey(USER_USERID)]
       public int UserId {
           get;set;
       }
  }
  </code></pre>
  
  <p>Now I would like to make queries to this. So I thought "Hey, let's use expressions to simplify for the end user on how to ask for stuff, yay."</p>
  
  <p>So, we could do something like EqualTo(o => o.user.UserId, 1);</p>
  
  <p>However, since the TableKey property differs from the reference key I need to first fetch from the database the userId from the Thread table and then with that Id start asking the User table for information with that id.</p>
  
  <p>Maybe this clarifies the purpose of all this, or maybe it don't.</p>