LINQ-To-DataSet匿名类型 - 双重转换为整数 - 为什么?

时间:2013-07-04 14:21:10

标签: vb.net linq-to-dataset

以下是我的简化课程。 我有两种方法从数据集通过LINQ获取一些数据。 然后我有GetTotalSales()方法来联合和汇总所有数据。 在最终的联合声明之前,我正在检查

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

和r.TYWeekSales是正确的 - 它是十进制的。 工会声明后:         昏暗的联盟= 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转换为整数。为什么?如何解决这个问题?

这是我全班同学:

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

结束班

0 个答案:

没有答案