用于自动映射器复杂映射的代码



自动映射器与 VB.NET

我在下面有以下课程。OrderA with list (of orderALineItem( 和 OrderBList with list (of orderB(。我想将数据从 OrderA 复制到 OrderBList。它将ItemName,ItemQty,价格从列表(OrderALineItem(复制到列表(OrderB(和OrderID,CustomerName从OrderA本身复制。我找到了几乎所有的 C# 代码,但无法将其转换为 vb.net 代码。

Public Class OrderA
Public Property OrderID As String
Public Property CustomerName As String
Public Property OrderLineItem As List(Of OrderALineItem)
End Class
Public Class OrderALineItem
Public Property ItemName As String
Public Property ItemQty As Integer
Public Property Price As Decimal
End Class
Public Class OrderBList
Public Property OrderBLineItem As List(Of OrderB)
End Class
Public Class OrderB
Public Property OrderID As String
Public Property CustomerName As String
Public Property ItemName As String
Public Property ItemQty As Integer
Public Property Price As Decimal
End Class

到目前为止,我 VB.NET 代码是:

Dim mapperConfiguration = New MapperConfiguration(Sub(config)
config.CreateMap(Of OrderALineItem, OrderBList)()
End Sub)
Dim mapper = mapperConfiguration.CreateMapper()
Dim objOrderB = mapper.Map(Of OrderBList)(objOrder.OrderLineItem)

上面的代码创建并将对象从 objOrder.OrderLineItem 复制到 OrderBList。就是这样。
谁能在 VB.NET帮助我解决这个问题。

注意:我是自动映射器中的全新内容

版本:自动映射器 6.2.2.0

我自己完成,我希望下面的代码对某人有所帮助。

Dim mapperConfiguration = New MapperConfiguration(Sub(config)
config.AddProfile(New CustomProfile_1)
End Sub)
Dim objMapper = mapperConfiguration.CreateMapper()
Dim objOrderB As List(Of Dest_OrderB) = objMapper.Map(Of Src_OrderA, List(Of Dest_OrderB))(objOrderA)
Public Class CustomProfile_1
Inherits Profile
Sub New()
CreateMap(Of Src_OrderALineItem, Dest_OrderB)()
CreateMap(Of Src_OrderA, List(Of Dest_OrderB))() _
.ConstructProjectionUsing(
Function(Src1) Src1.List_Src_OrderALineItem.Select(Function(Src2) New Dest_OrderB _
With {.CustomerName = Src1.CustomerName,
.OrderID = Src1.OrderID,
.ItemName = Src2.ItemName,
.ItemQty = Src2.ItemQty,
.Price = Src2.Price}
).ToList())
End Sub
End Class

最新更新