SolvedPrivate

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

Posted on 2014-11-20
5
28 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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
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 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Sorry Visual Basic, it should be newRow(0)
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
0
 

Author Closing Comment

by:JoeMiskey
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to implement Singleton Design Pattern in Java.

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now