title: Automapper: How do you map a type that is conditional?
body: <p>Greeting Gurus,</p>
  
  <p>So basically I have this a Map that I setup for use for TripFeeSchedule, trip fee schedule is a IEnumerable type. I am trying to map this so I do not have to have 5 or so ValueResolvers, and I am stuck.</p>
  
  <p>What I basically want to do is map a type of OPtional fee from a single fee schedule. So for example if the type is a late fee I want to map that specific type with price and fee being retrieved and displayed aptly. Because these fees are not always there I am trying to make this as simple as possible. </p>
  
  <pre><code>Mapper.CreateMap&lt;Trip, TripManagementViewModelCreateEdit&gt;().ForMember(dto =&gt; dto.TripOverride, opt =&gt; opt.ResolveUsing&lt;TripCompResolver&gt;().FromMember(x =&gt; x.TripComp))
  .ForMember(dto =&gt; dto.BusOverride, opt =&gt; opt.ResolveUsing&lt;TripCompResolver&gt;().FromMember(x =&gt; x.TransportComp)).ForMember(dto =&gt; dto.RentalSki, opt =&gt; opt.MapFrom&lt;TripFeeSchedule&gt;(x=&gt; x.ScheduledFees.First()) );
  </code></pre>
  
  <p>ScheduledFees dies on me, says Object Not Refrenced.</p>
  
  <p>I have the map and it seems to be working (debugger shows that its being called)</p>
  
  <p>[NullReferenceException: Object reference not set to an instance of an object.]
     AutoMapper.Mappers.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap) +393
     AutoMapper.Mappers.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper) +309
     AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) +221
     AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +723</p>
  
  <pre><code>[AutoMapperMappingException: Trying to map FreeLivinEnertainment.Core.TripFeeSchedule to FreeLivinEnertainment.Core.ViewModels.OptionalFee.
  Using mapping configuration for FreeLivinEnertainment.Core.TripFeeSchedule to FreeLivinEnertainment.Core.ViewModels.OptionalFee
  Destination property: RentalSki
  Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.]
     AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +811
     AutoMapper.Mappers.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap) +601
  
  [AutoMapperMappingException: Trying to map FreeLivinEnertainment.Core.TripFeeSchedule to FreeLivinEnertainment.Core.ViewModels.OptionalFee.
  Using mapping configuration for FreeLivinEnertainment.Core.TripFeeSchedule to FreeLivinEnertainment.Core.ViewModels.OptionalFee
  Destination property: RentalSki
  Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.]
     AutoMapper.Mappers.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap) +699
     AutoMapper.Mappers.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper) +309
     AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) +221
     AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +723
  
  [AutoMapperMappingException: Trying to map FreeLivinEnertainment.Core.Trip to FreeLivinEnertainment.Core.ViewModels.TripManagementViewModelCreateEdit.
  Using mapping configuration for FreeLivinEnertainment.Core.Trip to FreeLivinEnertainment.Core.ViewModels.TripManagementViewModelCreateEdit
  Exception of type 'AutoMapper.AutoMapperMappingException' was thrown.]
     AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +811
     AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType) +132
     AutoMapper.MappingEngine.Map(TSource source) +150
     AutoMapper.Mapper.Map(TSource source) +117
     FreeLivinEnertainment.Web.Controllers.Admin.TripsController.ShowEditTrip(Int32 id) in D:\Projects\jimbo\app\FreeLivinEnertainment.Web.Controllers\Controllers\Admin\TripManagementController.cs:138
     lambda_method(ExecutionScope , ControllerBase , Object[] ) +79
     System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
     System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +178
     System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +24
     System.Web.Mvc.&lt;&gt;c__DisplayClassd.&lt;InvokeActionMethodWithFilters&gt;b__a() +52
     System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254
     System.Web.Mvc.&lt;&gt;c__DisplayClassf.&lt;InvokeActionMethodWithFilters&gt;b__c() +19
     System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254
     System.Web.Mvc.&lt;&gt;c__DisplayClassf.&lt;InvokeActionMethodWithFilters&gt;b__c() +19
     System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +192
     System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +314
     System.Web.Mvc.Controller.ExecuteCore() +105
     System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +39
     System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
     System.Web.Mvc.&lt;&gt;c__DisplayClass8.&lt;BeginProcessRequest&gt;b__4() +34
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() +21
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _) +12
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +59
     System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +44
     System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +7
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8679150
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155
  </code></pre>