如何将int映射到布尔值

Ran*_*der 3 automapper

我正在使用AutoMapper 5.2.我目前有一个映射语句,如下所示:

 CreateMap<JeffreysOnline.Data.Customer, JeffreysOnline.Entities.Customer>()
                .ForMember(s => s.CustomerWant, t => t.Ignore());
Run Code Online (Sandbox Code Playgroud)

Customer表和Customer实体都有一个名为BadChecks的字段.在数据库中它是一个int.我最近在我的实体中将类型更改为bool.AutoMapper现在给我以下错误:

Unable to create a map expression from Customer.BadChecks (System.Int16) to Customer.BadChecks (System.Boolean) Mapping types: Customer -> Customer JeffreysOnline.Data.Customer -> JeffreysOnline.Entities.Customer Type Map configuration: Customer -> Customer JeffreysOnline.Data.Customer -> JeffreysOnline.Entities.Customer Property: BadChecks
Run Code Online (Sandbox Code Playgroud)

似乎AutoMapper不知道如何从int映射到布尔值.我可以用这个来帮助AutoMapper吗?

知道在我的DAL中,我正在使用ProjectTo()将IQueryable传递给另一个试图访问数据的方法,因此正在进行映射(生成错误),这可能会有所帮助.我的DAL代码如下所示:

return entityList.OrderBy(row => row.LastName).ProjectTo<Entities.Customer>();
Run Code Online (Sandbox Code Playgroud)

Les*_*k P 5

Automapper 6.0.2 - 无需任何ForMember工作... null,0 = false,值> = 1映射到true.

在Automapper 6.0.2中 - 其他方式:

    class nnnProfile : Profile
    {
        CreateMap<src, dst>()
            .ForMember(d => d.Decision, opt => opt.ResolveUsing<CustomBoolResolver>());    
    }
Run Code Online (Sandbox Code Playgroud)

解析器:

public class CustomBoolResolver : IValueResolver<src, dst, bool>
{
    public bool Resolve(src source, dst destination, bool destMember,
        ResolutionContext context)
    {
        return source.Decision == 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

但这是每个目的地,因此不够灵活.

根据这个页面:http: //taswar.zeytinsoft.com/automapper-mapping-objects-part-5-of-7-customresolver/

在过去,您可以编写仅具有源和目标类型的自定义解析程序.