繁体   English   中英

Automapper 配置文件将具有嵌套列表的对象映射到目标列表

[英]Automapper profile to map object with nested list to destination list

很难在标题中传达问题。 尝试确定编写自动映射器配置文件以在两个对象之间映射到列表的最佳方法,但目标列表中的项目数将等于源对象上内部列表中的项目数。 请找到我所追求的示例:

public class Destination
{
    public int Id { get; set; }
    public string Description { get; set; }
    public string Foo { get; set; }
    public string Bar { get; set; }
}

public class Source
{
   public int Id { get; set; }
   public string Description { get; set; }
   public List<FooBar> FooBar { get; set; }
}

public class FooBar
{
    public string Foo { get; set; }
    public string Bar { get; set; }
}

我想映射Source -> List<Destination> ,其中Destination中的项目数等于SourceFooBar的数量,但它们也都有来自SourceIdDescription

.CreateMap<Source, List<Destination>>()
                    .ConvertUsing(source => source.FooBar.Select(fb => new Destination
                    {
                        Foo = fb.Foo,
                        Bar = fb.Bar,
                        Description = source.Description,
                        Id = source.Id
                    }).ToList()
                )

显然,像这样使用它

var destination = mapper.Map<List<Destination>>(Source);

暂无
暂无

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

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