0

I've been tasked to take over some legacy reporting written in Visual Basic and have been tasked with updating some of the reports which the only way I can see is to use LINQ which I'm having a heck of a time with and I've spent the past week on Google trying to sort it out but I'm just missing "something".

What I have is a datatable that has like 10 columns and I need to group on two taking the results of the grouping and placing it into another datatable.

What I have so far is...

Dim Rep_Country_DataTable As New System.Data.DataTable
Dim Rep_Country_Group As IEnumerable = From Data_Row In Export_DataTable.Select("REP IS NOT NULL").AsEnumerable
                                       Group Data_Row By Rep = Data_Row.Field(Of String)("REP"),
                                                         Country = Data_Row.Field(Of String)("COUNTRY") Into Group
                                       Select Rep, Country

The LINQ works and I can see the grouped data in "Rep_Country_Group". My problem now is getting that data into the "Rep_Country_Group_DataTable" datatable, that's the part I can't figure out.

I would appreciate any help with this as I know it's something simple I'm missing.

  • 1
    Perhaps [Creating a DataTable From a Query (LINQ to DataSet)](https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/creating-a-datatable-from-a-query-linq-to-dataset) is what you're looking for. – Andrew Morton Sep 14 '20 at 14:18
  • 1
    I would go with FastMember: https://stackoverflow.com/a/564373/468973 – Magnus Sep 14 '20 at 14:24

1 Answers1

0

Thanks to those that replied. Using the replies I was able to refine my Google searches and found my solution.

Here's the revised code that solved my issue...

Dim Rep_Country_DataTable As New System.Data.DataTable
With DomainCountry_DataTable.Columns
   .Add("REP", GetType(String))
   .Add("COUNTRY", GetType(String))
End With
Dim Rep_Country_Group As IEnumerable(Of System.Data.DataRow) = (From Data_Row As System.Data.DataRow In Export_DataTable.Select("REP IS NOT NULL").AsEnumerable
                               Group Data_Row By Rep = Data_Row.Field(Of String)("REP"),
                                                 Country = Data_Row.Field(Of String)("COUNTRY") Into Group
                               Select New With {.Rep = Rep,
                                                .Country = Country}).Select(Function(new_row)
                                                                                Dim New_DataRow As System.Data.DataRow = DomainCountry_DataTable.NewRow()
                                                                                New_DataRow("REP") = new_row.Rep
                                                                                New_DataRow("COUNTRY") = new_row.Country
                                                                                Return New_DataRow
                                                                            End Function)