簡體   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