将匿名类型转换为强类型以进行分组查询?

时间:2010-08-30 19:53:36

标签: vb.net linq-to-sql anonymous-types

我把其他帖子中的一些信息拼凑在一起,但我被卡住了。

第一部分工作正常。基本上我使用LINQ查询数据库,然后循环生成报告的结果。

Dim results As System.Linq.IQueryable(Of Bud900Reports.tblBud_CONNECT)

If options.Type = reportType.Organization Then
        results = From p In db.tblBud_CONNECTs _
                Where p.TableOldName = "tblFY" & options.FiscalYear And p.DEPTID = options.GroupFilter _
                Order By p.SOURCE_CODE, p.ROW_CODE_903 _
                Select p



    ElseIf options.Type = reportType.Division Then
        'Here is my problem line

End If

For each result in results
    'loop through code generating report
Next

现在,如果reportType是Division,而不是有三个具有重复代码的函数,我想运行此查询并将其放入结果集中。

results = (From p In db.tblBud_CONNECTs _
                  Where p.TableOldName = "tblFY" & options.FiscalYear And p.DIVISION_CODE = options.GroupFilter _
                  Group p By p.DIVISION_CODE, p.SOURCE_CODE, p.ROW_CODE_903 Into _
                  OrigEft = Sum(p.OrigEft), OrigAmt = Sum(p.OrigAmt), ABEft = Sum(p.ABEft), ABAmt = Sum(p.ABAmt) _
                  Order By DIVISION_CODE, SOURCE_CODE, ROW_CODE_903 _
                  Select DIVISION_CODE, SOURCE_CODE, ROW_CODE_903, OrigEft, OrigAmt, ABEft, ABAmt)

这是刚刚分组和总结的相同数据。但它以匿名形式出现。我试着用“{.DIVISION_CODE = DIVISION_CODE,...}选择新的tblBud_CONNECTs”但是它给了我“不允许明确构造实体类型tblBud_CONNECTs”的错误。

我怎样才能做我想做的事?看来我应该能够。感谢。

2 个答案:

答案 0 :(得分:1)

为了完整起见,我会回答我自己的问题。

首先,我创建了一个类来保存结果。

Private Class results
    Private mDivCode As String
    Public Property DivCode() As String
        Get
            Return mDivCode

        End Get
        Set(ByVal value As String)
            mDivCode = value
        End Set
    End Property


    Private mSourceCode As Short
    Public Property SourceCode() As Short
        Get
            Return mSourceCode
        End Get
        Set(ByVal value As Short)
            mSourceCode = value
        End Set
    End Property


    Private mRowCode As Short
    Public Property RowCode() As Short
        Get
            Return mRowCode
        End Get
        Set(ByVal value As Short)
            mRowCode = value
        End Set
    End Property


    Private mOrigEft As Decimal
    Public Property OrigEft() As Decimal
        Get
            Return mOrigEft
        End Get
        Set(ByVal value As Decimal)
            mOrigEft = value
        End Set
    End Property


    Private mOrigAmt As Decimal
    Public Property OrigAmt() As Decimal
        Get
            Return mOrigAmt
        End Get
        Set(ByVal value As Decimal)
            mOrigAmt = value
        End Set
    End Property


    Private mABEft As Decimal
    Public Property ABEft() As Decimal
        Get
            Return mABEft
        End Get
        Set(ByVal value As Decimal)
            mABEft = value
        End Set
    End Property


    Private mABAmt As Decimal
    Public Property ABAmt() As Decimal
        Get
            Return mABAmt
        End Get
        Set(ByVal value As Decimal)
            mABAmt = value
        End Set
    End Property

End Class

然后我设置一个变量来保存结果。

Dim results As System.Linq.IQueryable(Of results)

然后我让我的linq查询填满了结果。

results = (From p In db.tblBud_CONNECTs _
                Where p.TableOldName = "tblFY" & options.FiscalYear And p.DEPTID = options.GroupFilter _
                Order By p.SOURCE_CODE, p.ROW_CODE_903 _
                Select New results With {.DivCode = p.DIVISION_CODE, .SourceCode = p.SOURCE_CODE.Value, .RowCode = p.ROW_CODE_903.Value, _
                                         .OrigEft = p.OrigEft.Value, .OrigAmt = p.OrigAmt.Value, .ABEft = p.ABEft.Value, .ABAmt = p.ABAmt.Value})

这就是我最终做我想做的事情。

答案 1 :(得分:0)

解决方案是创建Division实例的集合,并使用“results”填充此集合。
然后,您可以使用Attach方法为这些实体启用更改跟踪。