title: Can I use AutoMapper to map one list to another list of already instantiated objects?
body: <p>I am using this code to map a list of DTO's to their EF counterparts. I'd like to eliminate the loop, not to mention the fetch. The latter I can do by caching all <code>Employee</code> objects in a dictionary, but I'm stuck on the loop.</p>
  
  <pre><code>var gridEmps = (List&lt;EmployeeDescriptor&gt;) employeeDescriptorBindingSource.DataSource;
  Mapper.CreateMap&lt;EmployeeDescriptor, Employee&gt;();
  foreach (var newEmp in gridEmps)
  {
      var oldEmp = context.Employees.Single(emp =&gt; emp.Id == newEmp.Id);
      Mapper.Map(newEmp,oldEmp);
  }
  </code></pre>
  
  <p>As used above, AutoMapper maps from my DTO to an already instantiated entity object, but if I were to try this using two lists, like so, </p>
  
  <pre><code>var gridEmps = (List&lt;EmployeeDescriptor&gt;) employeeDescriptorBindingSource.DataSource;
  var dbEmps = context.Employees.ToList();
  
  Mapper.CreateMap&lt;EmployeeDescriptor, Employee&gt;();
  Mapper.Map(gridEmps, dbEmps);
  </code></pre>
  
  <p>how will AutoMapper know how to link the correct objects from one list to the other, based on their key property? I know the key property is also mapped, but surely this will mess with the EF change tracking?</p>
  
  <p>How can I tell AutoMapper to link source and destination objects by their key properties?</p>