SolvedPrivate

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

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

Accepted Solution

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Make all values in arrayist to titlecase+vb.net 23 27
Help with sorting data in Listbox using VB.NET 3 53
Resolve Dependency Issues 4 44
how to read json value 2 30
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 …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

911 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

22 Experts available now in Live!

Get 1:1 Help Now