LINQ group By
-
I am using LINQ to group by however after group By I need to create a new table with data... I am using Dataview to get all data
Dim vDependent As DataView = myDataTable.DefaultView
vDependent.RowFilter = "EEGRP ='" & item.EEGRP & "' AND EESSN ='" & item.EESSN & "'"Is there is a better way to do this?
Dim data = myDataTable.AsEnumerable().GroupBy(Function(r) New With {Key .EEGRP = r.Field(Of String)("EEGRP"), Key .EESSN = r.Field(Of String)("EESSN"), _ Key .DEDENF = r.Field(Of String)("DEDENF"), Key .DEDENM = r.Field(Of String)("DEDENM"), _
Key .DEDREL = r.Field(Of String)("DEDREL")},
Function(key, rows) New With
{
Key .EEGRP = key.EEGRP,
Key .EESSN = key.EESSN,
Key .DEDENF = key.DEDENF,
Key .DEDENM = key.DEDENM,
Key .DEDREL = key.DEDREL,
.Dates = rows.Select(Function(r) DateFromStringParts( _
r.Field(Of String)("DEIBCY"), _
r.Field(Of String)("DEIBYR"), _
r.Field(Of String)("DEIBMT"), _
r.Field(Of String)("DEIBDY"))).OrderByDescending(Function(d) d).Take(3).ToList()}
)'Create new table with original table schema Dim result As New DataTable result = myDataTable.Clone() For Each item In data Dim vDependent As DataView = myDataTable.DefaultView vDependent.RowFilter = "EEGRP ='" & item.EEGRP & "' AND EESSN ='" & item.EESSN & "'" ' item.Dates.ElementAtOrDefault(1), item.Dates.ElementAtOrDefault(2)) Dim newRow As DataRow = result.NewRow() newRow("EEGRP") = vDependent(0)("EEGRP")...