繁体   English   中英

'System.String' 和 'System.Nullable`1[System.DateTime]' 类型之间没有定义强制运算符

[英]No coercion operator is defined between types 'System.String' and 'System.Nullable`1[System.DateTime]'

我正在使用 AutoMapper 进行对象映射。 有时源值是空的,所以我想用“”代替它,而不是把空值放在数据库中。 源对象有许多可为空的数据类型,如 Date、Decimal 和 Int。 目前我正在使用

public class Invoice 
    {
        public Guid Id { get; set; }
        public string Number { get; set; }        
        public Contact Contact { get; set; }        
        public InvoiceType Type { get; set; }
        public InvoiceStatus Status { get; set; }
        public LineAmountType LineAmountTypes { get; set; }     
        public DateTime? Date { get; set; }        
        public DateTime? DueDate { get; set; }        
        public DateTime? ExpectedPaymentDate { get; set; }        
        public DateTime? PlannedPaymentDate { get; set; }        
        public decimal? SubTotal { get; set; }        
        public decimal? TotalTax { get; set; }        
        public decimal? Total { get; set; }        
        public decimal? TotalDiscount { get; set; 
        public decimal? CurrencyRate { get; set; }        
        public DateTime? FullyPaidOnDate { get; set; }        
        public decimal? AmountDue { get; set; }        
        public decimal? AmountPaid { get; set; }        
        public decimal? AmountCredited { get; set; }

    }

public partial class TempInvoice
    {
        public int RowId { get; set; }
        public System.Guid InvoiceID { get; set; }
        public string InvoiceNumber { get; set; }
        public Nullable<System.Guid> ContactID { get; set; }
        public string Type { get; set; }
        public string Status { get; set; }
        public string LineAmountType { get; set; }
        public Nullable<System.DateTime> InvoiceDate { get; set; }
        public Nullable<System.DateTime> DueDate { get; set; }
        public Nullable<System.DateTime> ExpectedPaymentDate { get; set; }
        public Nullable<System.DateTime> PlannedPaymentDate { get; set; }
        public Nullable<decimal> SubTotal { get; set; }
        public Nullable<decimal> TotalTax { get; set; }
        public Nullable<decimal> Total { get; set; }
        public Nullable<decimal> TotalDiscount { get; set; }
        public Nullable<decimal> CurrencyRate { get; set; }
        public Nullable<System.DateTime> FullyPaidOnDate { get; set; }
        public Nullable<decimal> AmountDue { get; set; }
        public Nullable<decimal> AmountCredited { get; set; }
    }


    public class InvoiceMapper : Profile
        {
            public InvoiceMapper()
            {
                CreateMap<Invoice, TempInvoice>()
                    .ForMember(des => des.PaymentID, map => map.MapFrom(src => src.Id))
                    .ForMember(des => des.InvoiceID, map => map.MapFrom(src => src.Invoice.Id))
                    .ForMember(des => des.PaymentDate, map => map.MapFrom(src => src.Date))
                    .ForAllMembers(src => src.NullSubstitute(""))//this doesn't work
                    ;
            }
        }

但我收到上述错误。 有没有办法可以排除数据时间类型并将 int 或 decimal 设置为 0 并将 string 设置为 "" ? 有没有可以用于所有数据类型的通用方法? 谢谢

您可以将解析器函数与 ResolveUsing 一起使用,使用您自己的自定义映射函数,如果目标是字符串,则返回空字符串,否则返回空字符串。

例如:

    private static object MyMapperResolver(object source, bool returnEmptyString = true) {
        return source ?? (returnEmptyString ? "" : source);
    }

    public static IMapper InitMappings() {
        Mapper.Initialize(cfg => {
            cfg.CreateMap<Invoice, TempInvoice>()
                .ForMember(des => des.PaymentID, map => map.ResolveUsing(src => MyMapperResolver(src.Id)))
                .ForMember(des => des.InvoiceID, map => map.ResolveUsing(src => MyMapperResolver(src.Invoice.Id)))
                .ForMember(des => des.PaymentDate, map => map.ResolveUsing(src => MyMapperResolver(src.Date, false)))
                ;
        });
        return Mapper.Instance;
    }

您可以使用AddTransform

public class InvoiceMapper : Profile
{
    public InvoiceMapper()
    {
        CreateMap<Invoice, TempInvoice>()
            .ForMember(des => des.PaymentID, map => map.MapFrom(src => src.Id))
            .ForMember(des => des.InvoiceID, map => map.MapFrom(src => src.Invoice.Id))
            .ForMember(des => des.PaymentDate, map => map.MapFrom(src => src.Date))
            .AddTransform<string>(s => string.IsNullOrWhiteSpace(s) ? "" : s); // this line
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM