Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. LINQ group By

LINQ group By

Scheduled Pinned Locked Moved Visual Basic
csharpdatabaselinqxmlquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    byka
    wrote on last edited by
    #1

    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")...
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups