在 Automapper CreateMap 和 Projection 中使用枚举作为条件
Using enums as criteria in Automapper CreateMap and Projection
使用 AutoMapper v6.1
无需硬编码该值来代替枚举 ((int)POStatusOptions.Ordered
),您如何使用投影完成此地图:
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)
)))
我的自动映射器配置使用的是配置文件。所以我有
我的配置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();
}
}
在 Application_Start()
中被称为:
AutoMapperConfiguration.Configure();
我的个人资料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)
)))
}
在 AutoMapper 中它被称为参数化。请参阅 AutoMapper doc。
你的情况是:
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)
)
)
你需要这样使用它:
dbContext.WorkOrderDetails.ProjectTo<WorkOrderDetailsListViewModel>(Config, new { poStatusOption = POStatusOptions.Ordered });
使用 AutoMapper v6.1
无需硬编码该值来代替枚举 ((int)POStatusOptions.Ordered
),您如何使用投影完成此地图:
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)
)))
我的自动映射器配置使用的是配置文件。所以我有
我的配置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();
}
}
在 Application_Start()
中被称为:
AutoMapperConfiguration.Configure();
我的个人资料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)
)))
}
在 AutoMapper 中它被称为参数化。请参阅 AutoMapper doc。
你的情况是:
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)
)
)
你需要这样使用它:
dbContext.WorkOrderDetails.ProjectTo<WorkOrderDetailsListViewModel>(Config, new { poStatusOption = POStatusOptions.Ordered });