title: Use Automapper to conditionally map from list
body: <p>I'm trying to use Automapper's overload of the Map function to combine two objects. One object is from the database, the other is my applications view model. The call to GetUserPreferences returns a list of key value paired preferences. I'd rather bind my MVC view to a strongly typed object, which sets the default preferences in its constructor. I figure AutoMapper can help with this. I just need to combine the two objects.</p>
  
  <pre><code>var dbPreferences = _preferenceManager.GetUserPreferences( HttpContext.User.Identity.Name, Session );            
  model = Mapper.Map(dbPreferences,model);
  </code></pre>
  
  <p>From my understanding, if I set up my mappings correctly, I can make this work. What I would like to happen is:</p>
  
  <ul>
  <li>If key/value pair does not exist in source, mapping ignores that property and leaves destination alone (destination is already set up with defaults)</li>
  <li>If key/value pair does exist in the source (preferences from db), mapping uses the source value and overwrites the destination's default value.</li>
  </ul>
  
  <p>I THOUGHT this was what Conditional mapping was suppposed to be used for. But I'm always getting the destination defaults. Even when the source has a value.</p>
  
  <p>My mappings are as follows:</p>
  
  <pre><code>Mapper.CreateMap&lt;List&lt;UserPreference&gt;, UserPreferencesModel&gt;( )
      .ForMember( dest =&gt; dest.VerticalResolution,
          opt =&gt;
              opt.Condition(
                  src =&gt;
                      src.SingleOrDefault( p =&gt; p.PreferenceName == PreferenceManager.VERTICAL_RESOLUTION_KEY ) != null
                  ) )
      .ForMember( dest =&gt; dest.GridRows,
          opt =&gt; 
              opt.Condition( 
                  src =&gt;
                      src.SingleOrDefault(p =&gt; p.PreferenceName == PreferenceManager.GRID_ROWS_KEY) != null ));
  </code></pre>
  
  <p>I know I could write my own method to do all of this, but I feel like I'm so close to how to do it with AutoMapper. Am I missing something? My src.SingleOrDefault result is never being used to map to when its not null. Why?</p>