我在将SourceModel数据映射到DestinationModel数据时遇到问题。DestinationModel具有复杂的对象类型。虽然名称匹配,但我没有看到任何数据被正确绑定。我是ValueInjector的新手,据我所知,这就是我尝试过的。
public class SourceModel
{
[Column("ctr_shname")]
public string CtrShname { get; set; }
[Column("reg_name")]
public string RegName { get; set; }
[Column("Male")]
public Int64 Male { get; set; }
[Column("Female")]
public Int64 Female { get; set; }
[Column("Single")]
public Int64 Single { get; set; }
[Column("Married")]
public Int64 Married { get; set; }
[Column("Divorced")]
public Int64 Divorced { get; set; }
[Column("Separated")]
public Int64 Separated { get; set; }
[Column("Widowed")]
public Int64 Widowed { get; set; }
}
public class DestinationModel
{
public string CtrShname { get; set; }
public string RegName { get; set; }
public Gender Genders { get; set; }
public MaritalStatus MaritalStatuses { get; set; }
}
public class Gender
{
public Int64 Male { get; set; }
public Int64 Female { get; set; }
}
public class MaritalStatus
{
public Int64 Single { get; set; }
public Int64 Married { get; set; }
public Int64 Divorced { get; set; }
public Int64 Separated { get; set; }
public Int64 Widowed { get; set; }
}这是我要映射的代码。
// get data from DB (row count 123)
IEnumerable<SourceModel> data = GetDataFromDB();
List<DestinationModel> finalAnswer = new List<DestinationModel>();
// Try 1: all properties are null for all 123 records
finalAnswer.InjectFrom(data);
// Try 2: Zero count. Nothing gets binds
var mapper1 = new MapperInstance();
finalAnswer = mapper1.Map<List<DestinationModel>>(data);请帮助我如何正确映射?
发布于 2019-01-24 06:49:33
我认为ValueInjector只允许注入一个对象,但是你可以做到这一点。
IEnumerable<SourceModel> data = GetDataFromDB();
IList<DestinationModel> finalAnswer = categoryList
.Select(x => new DestinationModel().InjectFrom(x)).Cast<DestinationModel>()
.ToList();或者执行foreach并注入每个对象:
foreach (var a in data)
{
finalAnswer.InjectFrom(a);
}https://stackoverflow.com/questions/54334519
复制相似问题