1

Using AutoMapper v6.1

Without having to hard code that value in lieu of the enum ((int)POStatusOptions.Ordered), how can you accomplish this map with Projection:

CreateMap<WorkOrderDetail, WorkOrderDetailsListViewModel>(MemberList.Destination)
.ForMember(vm => vm.QtyOnPOs, opt => opt.MapFrom(entity =>
    entity.Item.PODetails
        .Where(pod => pod.POHeader.StatusId >= (int)POStatusOptions.Ordered)
        .Sum(pod => pod.QtyOrdered)
        )))

My configuration for automapper is using profiles. So I have

My Config Class:

public static class AutoMapperConfiguration
{
    public static void Configure()
    {
        //see https://github.com/AutoMapper/AutoMapper/wiki/Configuration
        //see https://github.com/AutoMapper/AutoMapper/wiki/Configuration-validation 
        Mapper.Initialize(am =>
        {
            am.AddProfile<AutoMapperViewModelProfile>();
            am.AddProfile<AutoMapperViewModelProfileAdmin>();
        });

        //uncomment this during testing to get a list of all errors in the browser when you run any page in otis
        Mapper.AssertConfigurationIsValid();
    }    
}

Which is called in Application_Start() like: AutoMapperConfiguration.Configure();

My profile class:

public class AutoMapperViewModelProfile : Profile
{
    public AutoMapperViewModelProfile()
    {
        CreateMap<WorkOrderDetail, WorkOrderDetailsListViewModel>(MemberList.Destination)
        .ForMember(vm => vm.QtyOnPOs, opt => opt.MapFrom(entity =>
            entity.Item.PODetails
            .Where(pod => pod.POHeader.StatusId >= (int)POStatusOptions.Ordered)
            .Sum(pod => pod.QtyOrdered)
        )))
}
crichavin
  • 4,672
  • 10
  • 50
  • 95

1 Answers1

1

In AutoMapper it's called Parameterization. Please see AutoMapper doc.

In your case it would be:

POStatusOption poStatusOption = POStatusOption.Whatever;
CreateMap<WorkOrderDetail, WorkOrderDetailsListViewModel>(MemberList.Destination)
   .ForMember(
      vm => vm.QtyOnPOs, 
      opt => opt.MapFrom(entity =>
         entity.Item.PODetails
            .Where(pod => pod.POHeader.StatusId >= (int)poStatusOption)
            .Sum(pod => pod.QtyOrdered)
      )
   )

And you need to use it like this:

dbContext.WorkOrderDetails.ProjectTo<WorkOrderDetailsListViewModel>(Config, new { poStatusOption = POStatusOptions.Ordered });
dbraillon
  • 1,742
  • 2
  • 22
  • 34
  • Thanks, but I can't get this to work. (See my updated question for more details on my setup/config). When I try what you propose, I get the error `use of unassigned local varialble` for the `poStatusOptions` variable. Which I can understand as you declare the variable in this class, but don't explicitly assign it a value in this class...how would the compiler know you are passing in those values from another class? – crichavin Dec 15 '17 at 20:03
  • 1
    Just assign it to any value, I don't think it matter, AutoMapper will replace it – dbraillon Dec 15 '17 at 20:10