[英]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
中的項目數等於Source
中FooBar
的數量,但它們也都有來自Source
的Id
和Description
。
.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.