.net数据转换(一种对象类型到另一种) - 循环

时间:2011-04-22 16:03:36

标签: .net

我几个小时以来一直在反对这个问题,如果这里有人可以提供任何指示我会感激不尽。我想现在我被这个雪蒙住了,所以一组新的脑细胞可能会看到一个非常简单的解决方案!

基本上,我从遗留数据库中提取了一些数据,需要通过构建我们自己的Stock对象将其转换为我们的库存管理系统。

简化模型如下所示:

Public Class Stock
    Public Property Attributes As List(Of StockAttribute)
End Class

Public Class StockAttribute
    Public Property AttributeName As String
    Public Property AttributeValue As String
End Class

源数据作为此对象出现:

Public Class SourceOption
    Public Property OptionName As String
    Public Property OptionValue As String
End Class

可以使用以下方法构建一些示例数据:

Function GetSourceList1() As List(Of SourceOption)

    'prepare the source data
    Dim options As New List(Of SourceOption)

    Dim opt As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "Small"

    Dim opt2 As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "Medium"

    Dim opt3 As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "Large"

    Dim opt4 As New SourceOption
    opt.OptionName = "Colour"
    opt.OptionValue = "Black"

    Dim opt5 As New SourceOption
    opt.OptionName = "Colour"
    opt.OptionValue = "Purple"


    options.Add(opt)
    options.Add(opt2)
    options.Add(opt3)
    options.Add(opt4)
    options.Add(opt5)

    Return (options)

End Function


Function GetSourceList2() As List(Of SourceOption)

    'prepare the source data
    Dim options As New List(Of SourceOption)

    Dim opt As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "40"

    Dim opt2 As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "41"

    Dim opt3 As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "42"


    options.Add(opt)
    options.Add(opt2)
    options.Add(opt3)

    Return (options)

End Function

Function GetSourceList3() As List(Of SourceOption)

    'prepare the source data
    Dim options As New List(Of SourceOption)

    Dim opt As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "Small"

    Dim opt2 As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "Large"

    Dim opt3 As New SourceOption
    opt.OptionName = "Colour"
    opt.OptionValue = "Red"

    Dim opt4 As New SourceOption
    opt.OptionName = "Colour"
    opt.OptionValue = "Blue"

    Dim opt5 As New SourceOption
    opt.OptionName = "Style"
    opt.OptionValue = "A"

    Dim opt6 As New SourceOption
    opt.OptionName = "Style"
    opt.OptionValue = "B"

    options.Add(opt)
    options.Add(opt2)
    options.Add(opt3)
    options.Add(opt4)
    options.Add(opt5)
    options.Add(opt6)

    Return (options)

End Function

因此,我的“转换”方法需要获取SourceOption列表并构建Stock列表(带属性),如下所述:

Sub Convert()

'we need to convert the SourceOption list (in GetSourceList) into a list of Stock, each with a list of StockAttribute. So in the case of GetSourceList1 we would have
'six stock objects, each stock object containing two StockAttribute objects (one for colour and one for size):

'STOCK1 - Attribute1: Size:Small Attribute2: Colour:Black
'STOCK2 - Attribute1: Size:Medium Attribute2: Colour:Black
'STOCK3 - Attribute1: Size:Large Attribute2: Colour:Black
'STOCK4 - Attribute1: Size:Small Attribute2: Colour:Purple
'STOCK5 - Attribute1: Size:Medium Attribute2: Colour:Purple
'STOCK6 - Attribute1: Size:Large Attribute2: Colour:Purple

'in the case of GetSourceList2 we would have three stock objects with only one stockattribute each (for size):

'STOCK1 - Attribute1: Size:40
'STOCK2 - Attribute1: Size:41
'STOCK3 - Attribute1: Size:42

'in the case of GetSourceList3 we would have eigth stock objects with three stockattribute each (for size, colour and style):

'STOCK1 - Attribute1: Size:Small Attribute2: Colour:Red Attribute3: Style:A
'STOCK2 - Attribute1: Size:Large Attribute2: Colour:Red Attribute3: Style:A
'STOCK3 - Attribute1: Size:Small Attribute2: Colour:Blue Attribute3: Style:A
'STOCK4 - Attribute1: Size:Large Attribute2: Colour:Blue Attribute3: Style:A
'STOCK5 - Attribute1: Size:Small Attribute2: Colour:Red Attribute3: Style:B
'STOCK6 - Attribute1: Size:Large Attribute2: Colour:Red Attribute3: Style:B
'STOCK7 - Attribute1: Size:Small Attribute2: Colour:Blue Attribute3: Style:B
'STOCK8 - Attribute1: Size:Large Attribute2: Colour:Blue Attribute3: Style:B

End Sub

我希望这一点很清楚,在撕掉我所有的头发之前,任何反馈都会受到赞赏!

由于 卡尔

1 个答案:

答案 0 :(得分:1)

我认为你会发现在ValueInjecter上有一个look here很有帮助。它可以帮助您通过应用您可以定义的一些约定来从对象转换为对象。