views:

76

answers:

2

I've pieced together some information from other posts but I'm stuck.

The first part works fine. Basically I query the database using LINQ and then I loop through the results generating a report.

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

Now instead of having three function with alot of dupelicate code, if the reportType is Division I want to run this query and put it into the results set.

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)

It's the same data just grouped and summed up. But it comes through as an anonymous type. I tried to do "select new tblBud_CONNECTs with {.DIVISION_CODE = DIVISION_CODE, ...}" but it gave me the error of "Explicit construction of entity type tblBud_CONNECTs is not allowed".

How can I do what I want? It seems that I should be able to. Thanks.

A: 

The solution is to create a collection of the Division instances and populate this collection using 'results'.
Then you can use the Attach method to enable change tracking for these entities.

Devart
Can you explain in more detail? I don't understand.
Clint Davis
Try code like the following:Dim list As New List(Of Division)For Each element in results Dim div As New Division With { .DivisionCode = element.DIVISION_CODE, ..., .ABAmt = element.ABAmt}list.Add(div)NextDataContextInstance.Divisions.AttachAll(list)
Devart
Thanks for answering but I never did fully understand what you were trying to say. I ended up using a different method which I've added as an answer to this question.
Clint Davis
A: 

For completeness I'll answer my own question.

First I created a class to hold the results.

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

Then I set a variable to hold the results.

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

Then I made my linq query fill up the results like so.

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})

That's how I ended up doing what I wanted.

Clint Davis