开发者

Enumerate 'columns' in Entity Framework

开发者 https://www.devze.com 2023-02-02 02:43 出处:网络
we are dipping our toes with EF4 and have hit a wall. We have an application that manages member data and a core requirement is to generate letters using Word mail merge.

we are dipping our toes with EF4 and have hit a wall.

We have an application that manages member data and a core requirement is to generate letters using Word mail merge.

In the current imp开发者_C百科lementation we generate a dataset for the required records and enumerate it's columns and rows to create a tab delimited text file that Word can use to merge from. (This works really well, the Tab is used as there are multi-line results in the file such as FormattedAddressBlock).

With EF4 there is no way to convert internally to a dataset (this feature was dropped after the Orcas beta), so to the question:

How do we enumerate the columns and rows of a LINQ or ObjectQuery to allow us to build the text file.

Here is a sample of code we use with the datatable.

    Using wmTextWriter As TextWriter = New StreamWriter(WordMergeDataFile)

        ' Write the header row to the merge data file.
        Dim wmOutput As New StringBuilder()
        For i As Integer = 0 To MergeData.Columns.Count - 1
            wmOutput.Append(MergeData.Columns(i).ColumnName & vbTab)
        Next
        wmOutput.Remove(wmOutput.Length - 1, 1) ' Remove the trailing Tab character.
        wmTextWriter.WriteLine(wmOutput.ToString)

        ' Loop through the datatable and write out the data rows to the work merge file.
        wmOutput = New StringBuilder()
        With MergeData
            For iRowNo As Integer = 0 To .Rows.Count - 1
                For iColNo As Integer = 0 To .Columns.Count - 1
                    wmOutput.Append(DOUBLEQUOTE & .Rows(iRowNo).Item(iColNo).ToString & DOUBLEQUOTE & vbTab)
                Next
                wmOutput.Remove(wmOutput.Length - 1, 1) ' Remove the trailing Tab character.
                wmTextWriter.WriteLine(wmOutput.ToString)
                wmOutput = New StringBuilder()
            Next
        End With

        wmOutput = Nothing
        wmTextWriter.Close()

    End Using

Any help would be much appreciated.

Mark Harby Nottingham. UK

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号