2010-07-23 94 views
0

问候大师,Automapper:你如何映射一个有条件的类型?

所以基本上我有一个地图,我设置为TripFeeSchedule使用,旅行费用表是IEnumerable类型。我试图将其映射出来,所以我不必拥有5个左右的ValueResolver,并且我被卡住了。

我基本上想要做的是从单一费用计划中映射一种可选费用。因此,举例来说,如果类型是滞纳金,我想将特定类型与价格和费用进行匹配并正确显示。由于这些费用并不总是存在,我正试图尽可能简化。

Mapper.CreateMap<Trip, TripManagementViewModelCreateEdit>().ForMember(dto => dto.TripOverride, opt => opt.ResolveUsing<TripCompResolver>().FromMember(x => x.TripComp)) 
.ForMember(dto => dto.BusOverride, opt => opt.ResolveUsing<TripCompResolver>().FromMember(x => x.TransportComp)).ForMember(dto => dto.RentalSki, opt => opt.MapFrom<TripFeeSchedule>(x=> x.ScheduledFees.First())); 

ScheduledFees死在我身上,说Object Not Refrenced。

我有地图和它似乎是工作(调试器显示,其被调用)

[NullReferenceException异常:对象引用不设置为一个对象的一个​​实例] AutoMapper.Mappers.PropertyMapMappingStrategy.MapPropertyValue( ResolutionContext上下文,IMappingEngineRunner映射器,对象mappedObject,属性映射属性映射)393 AutoMapper.Mappers.PropertyMapMappingStrategy.Map(ResolutionContext上下文,IMappingEngineRunner映射器)309 AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext上下文,IMappingEngineRunner映射器)221 AutoMapper .MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext上下文) 723

[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.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a() +52 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254 
    System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>b__c() +19 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +254 
    System.Web.Mvc.<>c__DisplayClassf.<InvokeActionMethodWithFilters>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.<>c__DisplayClass8.<BeginProcessRequest>b__4() +34 
    System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21 
    System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>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& completedSynchronously) +155 

回答

0

想出如何做到这一点,我觉得自己像一个白痴,我想我可以只运行在拉姆达从会员,然后我可以做什么,我需要得到的结果回来。

opt => opt.ResolveUsing().FromMember(x => x.ScheduledFees.First()