简体   繁体   English

转换为DTO列表属性

[英]Convert to DTO list property

I have the following DTO: 我有以下DTO:

public class QuestionGroupDTO : IBaseDTO
{
    public string Description { get; set; }
    public string Header { get; set; }
    public Guid Id { get; set; }
    public int Order { get; set; }
    public IEnumerable<Services.Forms.Models.RelationForm_QuestionGroupDTO> RelationForms_QuestionGroups { get; set; }
    public IEnumerable<RelationQuestionGroup_QuestionDTO> RelationQuestionGroups_Questions { get; set; }
}

I have problem with the RelationQuestionGroups_Questions while converting. 转换时我对RelationQuestionGroups_Questions有问题。

Here Is how my RelationQuestionGroup_QuestionDTO looks like 这是我的RelationQuestionGroup_QuestionDTO样子

public class RelationQuestionGroup_QuestionDTO
{
    public int Order { get; set; }
    [Required]
    public Guid QuestionGroupId { get; set; }
    [Required]
    public Guid QuestionId { get; set; }

    public virtual QuestionGroupDTO QuestionGroup { get; set; }
    public virtual QuestionDTO Question { get; set; }
}

Here Is how I convert: 这是我的转换方式:

public static QuestionGroupDTO ToDTO(this QuestionGroup src)
{
    var dto = new QuestionGroupDTO
    {
        Id = src.Id,
        Header = src.Header,
        Description = src.Description,
        RelationQuestionGroups_Questions = src.RelationQuestionGroups_Questions.ToList()
    };
    return dto;
}

As you can see, I'm trying to just assign It and make a list of It, but I got a casting error here. 如您所见,我正在尝试仅分配它并列出它的列表,但是我在这里遇到了转换错误。 I'm not sure how to do this. 我不确定该怎么做。

I get the following error: 我收到以下错误:

Cannot implicity convert type Generic List to System.Collections.Generic.IEnumerble 无法将类型泛型列表隐式转换为System.Collections.Generic.IEnumerble

You're having a great start at mapping, but at RelationQuestionGroups_Questions = src.RelationQuestionGroups_Questions.ToList() , you're trying to assign a List<Entity> to List<Dto> . 您在映射方面有了一个良好的开端,但是在RelationQuestionGroups_Questions = src.RelationQuestionGroups_Questions.ToList() ,您正在尝试将List<Entity>分配给List<Dto> You can't do that. 你不能那样做。

You need to map any non-primitive properties as well. 您还需要映射任何非基本属性。 You can do that like this: 您可以这样做:

public static QuestionGroupDTO ToDTO(this QuestionGroup src)
{
    var dto = new QuestionGroupDTO
    {
        // ...
        RelationQuestionGroups_Questions = src.RelationQuestionGroups_Questions
                                              .Select(ToDTO)
                                              .ToList()
    };
    return dto;
}

Then you add a method to map RelationQuestionGroups_Question to RelationQuestionGroups_QuestionDTO : 然后添加一个方法来将RelationQuestionGroups_Question映射到RelationQuestionGroups_QuestionDTO

public RelationQuestionGroups_QuestionDTO ToDTO(RelationQuestionGroups_Question entity)
{
    return new RelationQuestionGroups_QuestionDTO
    {
        Order = entity.Order,
        // ...
    };
}

And then you'll go look at AutoMapper to automate this. 然后,您将看一下AutoMapper来自动执行此操作。

You forgot to map RelationQuestionGroups_Questions to RelationQuestionGroup_QuestionDTO . 您忘记了将RelationQuestionGroups_Questions映射到RelationQuestionGroup_QuestionDTO

public static QuestionGroupDTO ToDTO(this QuestionGroup src)
{
    var dto = new QuestionGroupDTO
    {
        Id = src.Id,
        Header = src.Header,
        Description = src.Description,
        RelationQuestionGroups_Questions = src.RelationQuestionGroups_Questions.Select(rq => new RelationQuestionGroup_QuestionDTO
            {
                Order = rq.Order,
                QuestionGroupId = rq.QuestionGroupId,
                QuestionId = rq.QuestionId
            }).ToList()
        };
    return dto;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM