Dapper, Lambda expressions and returning the correct mapped fields
<p>I have a project that does not allow us to change the SPROCS, but I wanted to use something to automatically map the datareader rows to a POCO class.
In came Dapper to the rescue. For the most part it works great, but in some cases where I need to populate nested classes.
For example, I have this model</p>

<pre><code>public class Account
{
    public string AccountNumber { get; set; }

    /* Removed for brevity */

    public Contact Contact { get; set; }
}

public class Contact
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
</code></pre>

<p>my respository just looked like this:</p>

<pre><code>/// &lt;summary&gt;
/// Lists a range of items
/// &lt;/summary&gt;
/// &lt;param name="storedProcedureName"&gt;The name of the SPROC to execute&lt;/param&gt;
/// &lt;param name="parameters"&gt;The parameters needed for the SPROC&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
public async Task&lt;IList&lt;T&gt;&gt; ListAsync(string storedProcedureName, OracleDynamicParameters parameters = null)
{
    return await ExecuteReaderAsync(storedProcedureName, parameters);
}

/// &lt;summary&gt;
/// Private method for executing a reader and returning the results as a list
/// &lt;/summary&gt;
/// &lt;param name="storedProcedureName"&gt;The name of the SPROC to execute&lt;/param&gt;
/// &lt;param name="parameters"&gt;The parameters needed for the SPROC&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
private async Task&lt;IList&lt;T&gt;&gt; ExecuteReaderAsync(string storedProcedureName, OracleDynamicParameters parameters = null)
{

    // Init command
    InitializeCommand(storedProcedureName, parameters);

    // Dynamically populate our list
    var list = await this._connection.QueryAsync&lt;T&gt;(storedProcedureName, parameters, commandType: CommandType.StoredProcedure);

    // Dispose
    DisposeCommand();

    // Return our list
    return list.ToList();
}
</code></pre>

<p>which can be called from any service a bit like this:</p>

<pre><code>return await Repository.ListAsync("IBPA_ORDERS.readLines", parameters);
</code></pre>

<p>Like I said, this works fine for a POCO class with no child classes, but as you can see, the <strong>Account</strong> model has a <strong>Contact</strong>.
Apparently you can get the contact populated using <strong>Lambda expressions</strong> like this:</p>

<pre><code>.Select(x =&gt; new Account
{
    AccountNumber = x.AccountNumber 
    Contact = new Contact 
    {
        FirstName = x.Firstname,
        LastName = x.LastName
    }
});
</code></pre>

<p>So, I am trying to update my Repository method to handle that.
So far I have this:</p>

<pre><code>/// &lt;summary&gt;
/// Private method for executing a reader and returning the results as a list
/// &lt;/summary&gt;
/// &lt;param name="storedProcedureName"&gt;The name of the SPROC to execute&lt;/param&gt;
/// &lt;param name="parameters"&gt;The parameters needed for the SPROC&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
private async Task&lt;IList&lt;T&gt;&gt; ExecuteReaderAsync(string storedProcedureName, OracleDynamicParameters parameters = null, Func&lt;dynamic, T&gt; query = null)
{

    // Init command
    InitializeCommand(storedProcedureName, parameters);

    // Get our models
    var models = await this._connection.QueryAsync(storedProcedureName, parameters, commandType: CommandType.StoredProcedure);

    // If we supply a query, use it. Otherwise cast the result as our type
    var results = query == null ? models.Select(m =&gt; (T)m) : models.Select(query);

    // Dispose
    DisposeCommand();

    // Return our list
    return results.ToList();
}
</code></pre>

<p>But I am not sure if that is correct.
I have tried to call it like this:</p>

<pre><code>// Execute and return our acctuons
return await Repository.ListAsync("IBPA_ACCOUNTSS.LIST", parameters, m =&gt; new Account {
    // LineRef = /* m is dynamic */
});
</code></pre>

<p>Because <strong>m</strong> is dynamic, I can't actually assign any properties.</p>

<p>Can someone help me out?</p>