2

Below is my simplified class. I have 2 methods that are getting some data from dataset through LINQ. Then I have GetTotalSales() method that unions and sums all data. Before final union statement I am checking data in

For Each r In O1
        Dim t = r.TYWeekSales ' so far so good - t is decimal
    Next

and r.TYWeekSales is correct - it is decimal. after union statement: Dim union = O1.Union(O2)

    Dim TotalSales2 = From u In union
                      Group u By _
                      Name = u.Name
                      Into Group _
                      Select TYWeekNetSale = Group.Sum(Function(u) u.TYWeekNetSale), _
                             LYWeekNetSale = Group.Sum(Function(u) u.LYWeekNetSale)

    For Each r2 In TotalSales2
        Dim t2 = r2.TYWeekNetSale ' no good - t2 is an integer
    Next

t2 is converted to integer. Why? How can this be fixed?

Here is my whole class:

Public Class Class1
    Private _Orders1 As IEnumerable
    Private _Orders2 As IEnumerable
    Private _DSNetSales As DataSet

Public Property Orders1() As IEnumerable
    Get
        Return _Orders1
    End Get
    Set(value As IEnumerable)
        _Orders1 = value
    End Set
End Property

Public Property Orders2() As IEnumerable
    Get
        Return _Orders2
    End Get
    Set(value As IEnumerable)
        _Orders2 = value
    End Set
End Property

Public Property DSNetSales() As DataSet
    Get
        Return _DSNetSales
    End Get
    Set(value As DataSet)
        _DSNetSales = value
    End Set
End Property

Private Sub GetSomething()
    Dim TYWeekSales = DSNetSales.Tables("T1").AsEnumerable()
    Dim TYWeekSalesData = From b In TYWeekSales
                Group b By _
                Name = b.Field(Of String)("loc")
                Into Group _
                Select Name,
                TYWeekNetSale = Group.Sum(Function(b) b.Field(Of Decimal)("net")), _
                LYWeekNetSale = CType(0.0, Decimal)

    Dim LYWeekSales = DSNetSales.Tables("T2").AsEnumerable()
    Dim LYWeekSalesData = From b In LYWeekSales
                Group b By _
                Name = b.Field(Of String)("loc")
                Into Group _
                Select Name,
                TYWeekNetSale = CType(0.0, Decimal), _
                LYWeekNetSale = Group.Sum(Function(b) b.Field(Of Decimal)("net"))

    Dim union = TYWeekSalesData.Union(LYWeekSalesData)

    Orders1 = From u In union
       Group u By _
       Name = u.Name
       Into Group _
       Select Name,
              TYWeekNetSale = Group.Sum(Function(u) u.TYWeekNetSale), _
              LYWeekNetSale = Group.Sum(Function(u) u.LYWeekNetSale)
End Sub


Private Sub GetSomethingElse()
    Dim TYWeekSales = DSNetSales.Tables("T3").AsEnumerable()
    Dim TYWeekSalesData = From b In TYWeekSales
                Group b By _
                Name = b.Field(Of String)("loc")
                Into Group _
                Select Name,
                TYWeekNetSale = Group.Sum(Function(b) b.Field(Of Decimal)("net")), _
                LYWeekNetSale = CType(0.0, Decimal)

    Dim LYWeekSales = DSNetSales.Tables("T4").AsEnumerable()
    Dim LYWeekSalesData = From b In LYWeekSales
                Group b By _
                Name = b.Field(Of String)("loc")
                Into Group _
                Select Name,
                TYWeekNetSale = CType(0.0, Decimal), _
                LYWeekNetSale = Group.Sum(Function(b) b.Field(Of Decimal)("net"))

    Dim union = TYWeekSalesData.Union(LYWeekSalesData)

    Orders2 = From u In union
              Group u By _
              Name = u.Name
              Into Group _
              Select Name,
                     TYWeekNetSale = Group.Sum(Function(u) u.TYWeekNetSale), _
                     LYWeekNetSale = Group.Sum(Function(u) u.LYWeekNetSale)
End Sub


Private Sub GetTotalSales()
    Dim O1 = From b In Orders1
    Dim O2 = From p In Orders2

    For Each r In O1
        Dim t = r.TYWeekSales ' so far so good - t is decimal
    Next

    For Each r In O2
        Dim t = r.TYWeekSales
    Next

    Dim union = O1.Union(O2)


    Dim TotalSales2 = From u In union
                      Group u By _
                      Name = u.Name
                      Into Group _
                      Select TYWeekNetSale = Group.Sum(Function(u) u.TYWeekNetSale), _
                             LYWeekNetSale = Group.Sum(Function(u) u.LYWeekNetSale)

    For Each r2 In TotalSales2
        Dim t2 = r2.TYWeekNetSale ' hmmm - t2 is an integer now
    Next

End Sub

End Class

4

0 回答 0