我目前在我的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可以年
首先,我保持简单,不将字段连接成字符串,我刚刚尝试使用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
但我不知道该怎么办!当从字符串转到int时,我可以做Convert.ToInt32,它就像一个款待。我是否需要创建自定义类型转换器?
我如何获得字符串prefix-jobnumber-year?
非常感谢。
更新
感谢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();
}
答案 0 :(得分:8)
我认为您可以使用ResolveUsing
方法来解决它。
Mapper.CreateMap<Job, JobViewModel>()
.ForMember(d => d.JobNumberFull,
exp =>
exp.ResolveUsing(j => string.Format("{0}-{1}-{2}", j.Prefix, j.JobNumber, j.Year)));
答案 1 :(得分:0)
您需要为从Job到JobViewModel的转换指定自定义转换函数。
Mapper.CreateMap<Job, JobViewModel>().ConvertUsing(
job => new JobViewModel {
JobNumberFull = string.Format("{0}-{1}-{2}",
job.Prefix, job.JobNumber, Job.Year)
});
要将Job转换为JobViewModel,请使用Map函数。
var jobViewModel = Mapper.Map<JobViewModel>(job);
要将作业列表转换为JobViewModel,请使用Select扩展名方法。
var jobViewModels = jobs.Select(job => Mapper.Map<JobViewModel>(job));