SolvedPrivate

VB.Net: Exporting Aggregate LINQ Query Results to Data Table (or Excel file)

Posted on 2014-11-20
5
31 Views
Last Modified: 2016-02-15
I am programming in VB.Net in Visual Studio 2010 (4.5 Framework).  I have a data table for which I have to create an Aggregate Query (grouping on multiple fields and summing multiple fields), and ultimately exporting my aggregated results to a new Excel file.

I was able to use LINQ to aggregate the records in my table, but am having problems writing those results to another blank, existing data table (I ultimately need to export the results to Excel, but didn't know if I can do that directly from my LINQ results so am trying to export the results to a table first).

I looped through the result set and returned my results back to the console, to confirm that part is working properly (and it is).  Here is my code:
    Private Sub PopulateRolledClaims()

        '   Aggregate records and sum amount fields
        Dim results = From c In EnhancedClaimsTable.AsEnumerable _
                    Group c By SSN = c.Field(Of String)("SSN"), _
                    FirstName = c.Field(Of String)("FirstName") _
                    Into g = Group _
                    Select New With _
                           { _
                               .SSN = SSN, _
                               .FirstName = FirstName, _
                               .TotalCopayAmt = g.Sum(Function(c) _
                                    c.Field(Of Decimal)("CopayAmount")), _
                                .TotalDeductibleAmt = g.Sum(Function(c) _
                                     c.Field(Of Decimal)("DeductibleAmount")) _
                            }

        '   Display results in console
        For Each c In results
            Console.WriteLine("SSN = {0} " & vbTab & _
                              "FirstName = {1} " & vbTab & _
                              "TotalCopayAmt = {2} " & vbTab & _
                              "TotalDeductibleAmt = {3} ", _
                              c.SSN, c.FirstName, c.TotalCopayAmt, c.TotalDeductibleAmt)
        Next

        ''   Write results of all rows to table table
        For Each row In results
            RolledClaimsTable.ImportRow(row)    'THIS IS WHERE MY ERROR IS
        Next

    End Sub

Open in new window

On the line of code where I have commented "THIS IS WHERE MY ERROR IS", it is underlining the word "row" and when I hover over it, it says:
"Value of type ' <anonymous type> (line 251) cannot be converted to 'System.Data.DataRow'."

Note that the fields in my "RolledClaimsTable" table have the same name as the fields in the Aggregate Query.

I have used this ".ImportRow(row)" methodology to successfully populate other Data Tables from LINQ queries in the past, but they were never Aggregate Queries.  I am guessing that changes things and I have to use a different method to populate the table.

Can anyone help?

Thanks
0
Comment
Question by:JoeMiskey
  • 2
  • 2
5 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40456121
Hi Joe;

The ImportRow of the DataTable object takes a DataRow object. The query is returning a Anonymous data type and therefore you can't you that method. It looks like the RolledClaimsTable DataTable is already created with all its columns. The way you can do it in that For Each loop is to get a new row from RolledClaimsTable and fill in the corresponding values from the results of the query. Something like the following.

For Each row In results
    Dim newRow As DataRow = RolledClaimsTable.NewRow()
    newRow[0] = row.SSN
    newRow[1] = ...
    ...
    RolledClaimsTable.Rows.Add(newRow)
Next

Open in new window

0
 

Author Comment

by:JoeMiskey
ID: 40456158
Fernando,

When I tried that code, it underlines "newRow[0]" and says "Expression is not a method".

Note, if it is easier, I don't need to create a empty DataTable object first and then populate it in this manner.  All I am trying to do is take the results of this query, and export it to an Excel file, one way or another.  So if there are different/easier ways of getting from my LINQ query to the final Excel file, I am open to them.

Thanks
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40456361
Sorry Visual Basic, it should be newRow(0)
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40456491
0
 

Author Closing Comment

by:JoeMiskey
ID: 40457182
Thanks Fernando, that works.

Eric, that looks like it works in that it creates a data table on the fly, which is nice.  A bit more work, but powerful.  Since I already have the table shell, I didn't need to go that route, but I will definitely file this away, as I can envision having to use this for other projects.  Thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question