我无法将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);
请帮助我如何正确映射?
答案 0 :(得分:1)
我认为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);
}