自动映射器:将整数转换为字符串

本文关键字:整数 转换 字符串 映射 | 更新日期: 2023-09-27 18:31:01

我目前正在我的 c# MVC 项目中使用 AutoMapper,并且遇到了从整数转换为字符串的问题。

我有两个类:

public class Job
{
    public string Prefix { get; set; }
    public int JobNumber { get; set; }
    public int Year { get; set }
    public int JobPriority { get; set; }
    public virtual EntityPriority EntityPriority { get; set; }
}
public class JobViewModel
{
    public string JobNumberFull { get; set; }
{

使用Automapper,我想连接Job 类中的所有三个属性以形成一个字符串,然后将其映射到 JobViewModel 类中的 JobNumberFull。

前缀-作业编号-年份

首先,我

保持简单,没有将字段连接成一个字符串,我只是尝试采用 JobNumber (int) 并将其映射到 JobNumberFull(字符串),如下所示:

Mapper.CreateMap<int, string>().ConvertUsing(Convert.ToString);
Mapper.CreateMap<Job, JobViewModel>()
    .ForMember(d => d.JobPriorityId, opt => opt.MapFrom(s => s.EntityPriority.EntityPriorityID))
    .ForMember(d => d.JobPriorityLevel, opt => opt.MapFrom(s => s.EntityPriority.PriorityLevel))
    .ForMember(d => d.JobNumberFull, opt => opt.MapFrom(s => s.JobNumberYear));
Mapper.AssertConfigurationIsValid();
var jobsList = db.Jobs.Where(j => j.OperationID == operationId).Project().To<JobViewModel>().ToList();

当我运行这个时,我收到以下错误

Type 'System.String' does not have a default constructor

我知道这与

Convert.ToString

但我不知道该怎么办!当从字符串转到整数时,我可以做 Convert.ToInt32,它就像一种享受。是否需要创建自定义类型转换器?

我将如何获取字符串前缀-作业编号-年份?

所有的帮助非常感谢。


更新

感谢Jeremy和AutoMapper网站的帮助,我设法让它在这里工作是我的解决方案,如果您认为有更优雅的解决方案,请随时发表评论:

public class Job 
{
    public string Prefix { get; set; }
    public int JobNumber { get; set; }
    public int Year { get; set; }
    public int JobPriority { get; set; }
    public virtual EntityPriority EntityPriority { get; set; }
}
public class JobViewModel 
{
    public int JobNumber { get; set; }
    public int Year { get; set; }
    public string JobNumberFull { get; set; }
}
public enum EntityPriority 
{
  Normal = 0,
  High
}
public interface IValueResolver
{
   ResolutionResult Resolve(ResolutionResult source);
}
public class JobNumberConverter : ValueResolver<Job, string>
{
    protected override string ResolveCore(Job source)
    {
        return string.Format("{0}-{1}-{2}", source.Prefix, source.JobNumber, source.Year);
    }
}
public ActionResult Index()
{
    Mapper.CreateMap<Job, JobViewModel>()
        .ForMember(jvm => jvm.JobNumberFull, opt => opt.ResolveUsing<JobNumberConverter>());
    Mapper.AssertConfigurationIsValid();
    List<Job> jobs = new List<Job>();
    Job j = new Job()
    {
        JobNumber = 1,
        Prefix = "prefix",
        Year = 2013,
        EntityPriority = EntityPriority.High,
        JobPriority = 2
    };
    Job j2 = new Job()
    {
        JobNumber = 2,
        Prefix = "prefix",
        Year = 2013,
        EntityPriority = EntityPriority.High,
        JobPriority = 2
    };
    Job j3 = new Job()
    {
        JobNumber = 3,
        Prefix = "prefix",
        Year = 2013,
        EntityPriority = EntityPriority.High,
        JobPriority = 2
    };
    jobs.Add(j);
    jobs.Add(j2);
    jobs.Add(j3);
    var jobViewModels = jobs.Select(job => Mapper.Map<JobViewModel>(job));
    return View();
}

自动映射器:将整数转换为字符串

我认为您可以使用ResolveUsing方法来解决它。

Mapper.CreateMap<Job, JobViewModel>()
    .ForMember(d => d.JobNumberFull,
    exp =>
    exp.ResolveUsing(j => string.Format("{0}-{1}-{2}", j.Prefix, j.JobNumber, j.Year)));

您需要为从作业到作业视图模型的转换指定自定义转换函数。

Mapper.CreateMap<Job, JobViewModel>().ConvertUsing(
  job => new JobViewModel {
    JobNumberFull = string.Format("{0}-{1}-{2}",
                      job.Prefix, job.JobNumber, Job.Year)
  });

要将作业转换为 JobViewModel,请使用映射函数。

var jobViewModel = Mapper.Map<JobViewModel>(job);

若要将作业列表转换为 JobViewModels,请使用"选择扩展"方法。

var jobViewModels = jobs.Select(job => Mapper.Map<JobViewModel>(job));