很難在標題中傳達問題。試圖確定撰寫自動映射器組態檔以在兩個物件之間映射到串列的最佳方法,但目標串列中的專案數將等于源物件上內部串列中的專案數。請找到我所追求的示例:
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
等于 in 的數量,FooBar
但Source
它們每個都有一個Id
和Description
來自Source
.
uj5u.com熱心網友回復:
.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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/483517.html
上一篇:如何在2D串列中添加值