title: Entity Framework Core: how to use result set of stored procedure and map it to object and object of object by maintaining async
body: <p>I'm working on Entity Framework Core.</p>
  
  <p>My task is to execute a stored procedure and populate the DTO based on the result of that stored procedure. I'm having difficult to map the properties of DTO class with stored procedure result especially when I've to map it object (another class type) property.</p>
  
  <p>Consider the following DTO classes</p>
  
  <pre><code>public class Contact
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public string EmailAddress { get; set; }
      public Company Company { get; set; }
  }
  
  public class Company
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>My SQL query for stored procedure is:</p>
  
  <pre><code>SELECT
      Id                  = contact.id,
      Name                = LTRIM(RTRIM(contact.name)),
      EmailAddress        = LTRIM(RTRIM(contact.email)),
      -- company information
      CustomerId          = contact.cust_id
      CustomerName        = customer.name
  FROM
      contact 
  INNER JOIN
      customer ON contact.cust_id = customer.cust_id
  </code></pre>
  
  <p>I'm using NuGet package <code>StoredProcedureEFCore</code> to perform execution of stored procedure.</p>
  
  <p>Here is the code to call the stored procedure:</p>
  
  <pre><code>Task&lt;Contact&gt; contactDetails = null;
  
  _context.LoadStoredProc("usp_GetContactDetails")
          .Exec(r =&gt;
          {
              contactDetails = r.FirstOrDefaultAsync&lt;Contact&gt;();
          });
  
  // TODO: need to populate Company object
  </code></pre>
  
  <p>This code work for Id, Name, EmailAddress but not for Company property as I've to go company object and populate that object properties.</p>
  
  <p>Based on my research on <code>StoredProcedureEFCore</code>, we can do this using <code>ToDictionaryAsync</code> or <code>ToLookupAsync</code> but I'm still not able to do that.</p>
  
  <p>Plus, one more thing I've to do that as asynchronously.</p>