首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ValueInjecter未正确绑定属性

ValueInjecter未正确绑定属性
EN

Stack Overflow用户
提问于 2019-01-24 03:39:58
回答 1查看 73关注 0票数 0

我在将SourceModel数据映射到DestinationModel数据时遇到问题。DestinationModel具有复杂的对象类型。虽然名称匹配,但我没有看到任何数据被正确绑定。我是ValueInjector的新手,据我所知,这就是我尝试过的。

代码语言:javascript
复制
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; }
}

这是我要映射的代码。

代码语言:javascript
复制
// 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);

请帮助我如何正确映射?

EN

回答 1

Stack Overflow用户

发布于 2019-01-24 06:49:33

我认为ValueInjector只允许注入一个对象,但是你可以做到这一点。

代码语言:javascript
复制
   IEnumerable<SourceModel> data = GetDataFromDB();
IList<DestinationModel> finalAnswer = categoryList
    .Select(x => new DestinationModel().InjectFrom(x)).Cast<DestinationModel>()
    .ToList();

或者执行foreach并注入每个对象:

代码语言:javascript
复制
foreach (var a in data)
{
    finalAnswer.InjectFrom(a);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54334519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档