简体   繁体   English

AutoMapper:string to nullable int

[英]AutoMapper: string to nullable int

This is similar to a previous post of mine ( AutoMapper: Int to String and back again ), but it touches on another aspect this time with a nullable int. 这类似于我以前的帖子( AutoMapper:Int to String再回来 ),但这次涉及另一个方面,可以使用nullable int。

My Business Object 我的业务对象

public class Employee 
{
    ....
    public int? ReportsToEmployeeId {get; set;}
    ....
}

My WCF 我的WCF

[DataContract(Name="Employee", Namespace="url")]
public class Employee
{
    ....
    [DataMember(Order = 1)]
    public string ReportsToEmployeeId {get; set;}
    ....
}

Mapper 映射器

Mapper.CreateMap<Employee.Entity.Employee, Employee.Service.Entity.Employee>()
    ....
    .ForMember(dest => dest.ReportsToEmployeeId, 
        opt => opt.MapFrom(src => src.ReportsToEmployeeId.HasValue ?
            src.ReportsToEmployeeId.Value.ToString("D9") :
            string.Empty))
    ....
    .ReverseMap() 
    ....
    .ForMember(dest => dest.ReportsToEmployeeId, 
        opt => opt.MapFrom(src => string.IsNullOrWhitespace(src.ReportsToEmployeeId) ? 
          null : 
          int.Parse(src.ReportsToEmployeeId)))

I use the first opt.MapFrom to convert from integer to string and supply leading zeros if necessary. 我使用第一个opt.MapFrom从整数转换为字符串,并在必要时提供前导零。 The second uses an extension to test whether the string value is null or blank and assigns it to a null or parses to an integer to fill the nullable int. 第二个使用扩展来测试字符串值是null还是空白并将其赋值为null或解析为整数以填充可为空的int。 However I get a build error on the null segment. 但是我在null段上遇到了构建错误。 The error is simply stating that it has an unknown method for ForMember or MapFrom. 该错误只是声明它具有ForMember或MapFrom的未知方法。

Visual Studio does confirm that dest.ReportsToEmployeeId is a nullable int in the code after the ReverseMap, so it seems strange that it will not take it. Visual Studio确实在ReverseMap之后的代码中确认dest.ReportsToEmployeeId是一个可以为null的int,所以看起来很奇怪它不会接受它。 If I change the code to be: 如果我将代码更改为:

....
.ForMember(dest => dest.ReportsToEmployeeId, 
        opt => opt.MapFrom(src => string.IsNullOrWhitespace(src.ReportsToEmployeeId) ? 
          int.Parse(src.ReportsToEmployeeId) : 
          int.Parse(src.ReportsToEmployeeId)))
....

it seems to build okay. 它似乎建立好。 So I know it is the mapping of the null to the nullable int causing the issue. 所以我知道它是null到可空int的映射导致问题。

What am I missing? 我错过了什么? Would there be a better way to approach this? 有没有更好的方法来解决这个问题?

I think the error you're seeing is hiding the actual problem, which is that the compiler can't determine what type you want out of the conditional: 我认为你看到的错误是隐藏了实际的问题,即编译器无法确定你想要的条件类型:

string.IsNullOrWhiteSpace(src.ReportsToEmployeeId) ?
    null :
    int.Parse(src.ReportsToEmployeeId)

Here, the compiler does not know that you want a Nullable<int> as a result of evaluating the conditional operator. 这里,编译器不知道你想要一个Nullable<int>作为评估条件运算符的结果。 null doesn't have a type, so you need to cast it to int? null没有类型,所以你需要将它int?int? :

string.IsNullOrWhiteSpace(src.ReportsToEmployeeId) ?
    (int?)null :
    int.Parse(src.ReportsToEmployeeId)

Or you could use default(int?) , or new int?() , or cast the result of int.Parse to an int? 或者您可以使用default(int?)new int?() ,或将int.Parse的结果转换为int? .

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

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