c# - AutoMapper 复杂类型对象映射 C#

标签 c# automapper complextype

在我的方法中,我得到了包含多个字符串列表和复杂类型对象列表的结果。

            var AcctLst = gd.validateGroupMembershipUploadDetails(_input);


            Mapper.CreateMap<Data.Entities.Upload.GroupMembershipValidationOutput, Business.Upload.GroupMembershipValidationOutput>();

            var result = Mapper.Map<Data.Entities.Upload.GroupMembershipValidationOutput, Business.Upload.GroupMembershipValidationOutput>(AcctLst);


            return result;

var AcctLst 包含示例数据:

enter image description here

Data.Entities.Upload.GroupMembershipValidationOutput

Business.Upload.GroupMembershipValidationOutput

看起来像

public class GroupMembershipValidationOutput
{
    public List<string> _validMasterIds { get; set; }
    public List<ChapterCodeValidationOutput> _validChapterCodes { get; set; }
    public List<GroupCodeValidationOutput> _validGroupCodes { get; set; }

    public List<string> _invalidMasterIds { get; set; }
    public List<string> _invalidChapterCodes { get; set; }
    public List<string> _invalidGroupCodes { get; set; }
}


public class ChapterCodeValidationOutput
{
    public string chpt_cd { get; set; }
    public string appl_src_cd { get; set; }
}



public class GroupCodeValidationOutput
{
    public string grp_cd { get; set; }
    public string grp_nm { get; set; }
}

我猜 _validChapterCodes 和 _validGroupCodes 抛出以下异常:

Missing type map configuration or unsupported mapping.

Mapping types:
ChapterCodeValidationOutput -> ChapterCodeValidationOutput
ARC.Donor.Data.Entities.Upload.ChapterCodeValidationOutput -> ARC.Donor.Business.Upload.ChapterCodeValidationOutput

Destination path:
GroupMembershipValidationOutput._validChapterCodes._validChapterCodes._validChapterCodes0[0]

Source value:
ARC.Donor.Data.Entities.Upload.ChapterCodeValidationOutput

最佳答案

是的,该死。这相对简单。您只需首先映射内部嵌套类型即可。

    var AcctLst = gd.validateGroupMembershipUploadDetails(_input);
    Mapper.CreateMap<Data.Entities.Upload.ChapterCodeValidationOutput, Business.Upload.ChapterCodeValidationOutput>();
    Mapper.CreateMap<Data.Entities.Upload.GroupCodeValidationOutput, Business.Upload.GroupCodeValidationOutput>();
    Mapper.CreateMap<Data.Entities.Upload.GroupMembershipValidationOutput, Business.Upload.GroupMembershipValidationOutput>();

    var result = Mapper.Map<Data.Entities.Upload.GroupMembershipValidationOutput, Business.Upload.GroupMembershipValidationOutput>(AcctLst);
    return result;

然后就可以完美运行了。

关于c# - AutoMapper 复杂类型对象映射 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37651231/

相关文章:

c# - 使用 lambda 查询获取前 5 个值

c# - 如何将字符串映射到自动映射器中的日期?

python - 如何在 python 3 中将复杂的数据帧值转换为 float ?

C# 泛型 & 不会发疯

c# - ASP.NET Core 在 ExternalLoginSignInAsync 之后获取 UserId

c# - UseNpgsql 在 .NET Core 的 IServiceCollection 中不可用

asp.net-core - ASP.NET Core MVC 中的 AutoMapper 实现

c# - 具有嵌套多个类对象的自动映射器类对象

sql-server - 右侧带有不必要空格的 SQL 数据

entity-framework - 从 Azure 移动应用获取具有嵌套复杂类型的模型