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

时间:2016-08-09 05:49:36

标签: c# .net automapper nullable coercion

我正在使用AutoMapper进行对象映射。有时源值为null所以我想用""替换它。而不是在数据库中放置NULL。 源对象有许多可以为空的数据类型,如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,将字符串设置为&#34;&#34; ?是否有可用于所有数据类型的通用方法? 感谢

1 个答案:

答案 0 :(得分:0)

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

例如:

    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;
    }