title: C Sharp Generic Dapper Query Joining two tables
body: <p>I am a c# noobie, but here is the gist of what I am trying to do: </p>
  
  <pre><code>    public IEnumerable&lt;T&gt; GetAll&lt;T, K&gt;(string schemaName) where T : GenericModel
      {
          var sql = @"SELECT * from " +
              schemaName + "." + T.getTableName() + " primaryTable LEFT JOIN " +
              schemaName + "." + K.getTableName() +
              " ForeignTable on ForeignTable.id = primaryTable." + T.getForeignFieldName();
  
          return _connection.Query&lt;T, K, T&gt;(sql, (primary, foreign) =&gt;
          { (primary = T.getForeignFieldName()) = foreign; return primary; });
      }
  </code></pre>
  
  <p>As an example T= Employee and K = Person</p>
  
  <pre><code>    public IEnumerable&lt;Employee&gt; GetAll(string schemaName)
      {
          var sql = @"SELECT * from " + schemaName + 
              ".Employee primaryTable LEFT JOIN " + 
              schemaName + ".Person ForeignTable "+
              "on ForeignTable.id = primaryTable.person_id";
          return _connection.Query&lt;EmployeeModel, PersonModel, EmployeeModel&gt;
              (sql, (primary, foreign) =&gt; { primary.person = foreign; return primary; });
      }
  </code></pre>
  
  <p><code>GenericModel</code> has the <code>getForeignFieldName()</code> and <code>getTableName()</code> which are shadowed by the derived classes (<code>Employee</code> and <code>Person</code>)</p>
  
  <p>The example Models might look like: </p>
  
  <pre><code>Person {
    int id;
    string Name;
    string address;
    int age;
  }
  
  Employee {
    int id;
    int person_id;
    Person person;
    double salary;
  </code></pre>
  
  <p>}</p>
  
  <p>Of course the generic solution doesn't work and I'm aware of sql injection. I just want to a generic method that I can use the way I have described with Dapper's Query() function. </p>
  
  <p>In other words, how do you write a generic function that can do this: <a href="https://github.com/StackExchange/dapper-dot-net#multi-mapping" rel="nofollow noreferrer">Dapper's Multi Mapping</a></p>