Results 1 to 11 of 11

Thread: [RESOLVED] Sorting a datatable out?

  1. #1

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Resolved [RESOLVED] Sorting a datatable out?

    All,

    I have a problem which I am struggling to efficiently put into code. I have a datatable

    ID | NAME | DATE | JOB
    0 | Joe | 01/01 | Selling
    1 | Bob | 03/01 | Buying
    2 | Joe | 02/01 | Manager
    3 | John | 01/01 | Buying

    How can i programmatically get it to only show a unique list of names and the if there is a duplicate name to only show the latest entry, ie:

    ID | NAME | DATE | JOB
    1 | Bob | 03/01 | Buying
    2 | Joe | 02/01 | Manager
    3 | John | 01/01 | Buying

    Anyone have any ideas?

    Cheers,

    Ken
    If you find my thread helpful, please remember to rate me

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Sorting a datatable out?

    "...get it to only show..."

    Do you mean you want a DataView representing the filtered rows? Or do you want to run a query on the DataTable (LINQ) to return the specified rows?

  3. #3

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Sorting a datatable out?

    I just want to put it in another datatable.
    If you find my thread helpful, please remember to rate me

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Sorting a datatable out?

    Latest entry based on date right?

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Public Sub New()
    4.         Me.InitializeComponent()
    5.  
    6.         Dim table As New DataTable()
    7.  
    8.         With table
    9.             With .Columns
    10.                 .Add("ID", GetType(Integer))
    11.                 .Add("Name", GetType(String))
    12.                 .Add("Date", GetType(Date))
    13.                 .Add("Job", GetType(String))
    14.             End With
    15.             With .Rows
    16.                 .Add(0, "Joe", #1/1/2010#, "Selling")
    17.                 .Add(1, "Bob", #1/1/2010#, "Buying")
    18.                 .Add(2, "Joe", #2/1/2010#, "Manager")
    19.                 .Add(3, "John", #1/1/2010#, "Buying")
    20.             End With
    21.         End With
    22.  
    23.         Dim results = table.AsEnumerable().OrderBy(Function(row) row.Field(Of Date)("Date")) _
    24.                                           .GroupBy(Function(row) row.Field(Of String)("Name")) _
    25.                                           .Select(Function(grouping) grouping.Last()).CopyToDataTable()
    26.  
    27.         Me.DataGridView1.DataSource = results
    28.  
    29.     End Sub
    30.  
    31. End Class

  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Sorting a datatable out?

    Ahhhh just saw that it was VB2005!! Disregard my post...

  6. #6

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Sorting a datatable out?

    Bugger, can it not be done in VS2005?

    If you find my thread helpful, please remember to rate me

  7. #7
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Sorting a datatable out?

    Quote Originally Posted by dinosaur_uk View Post
    Bugger, can it not be done in VS2005?

    Not with LINQ... Upgrade?

    Kidding, I'll think about the logic for VB2005.

  8. #8

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Sorting a datatable out?

    Thanks dude,

    my code at the moment is crazily long....and it still doesnt work!
    If you find my thread helpful, please remember to rate me

  9. #9
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Sorting a datatable out?

    Quote Originally Posted by dinosaur_uk View Post
    Thanks dude,

    my code at the moment is crazily long....and it still doesnt work!
    Try this:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Public Sub New()
    4.         Me.InitializeComponent()
    5.  
    6.         Dim table As New DataTable()
    7.         Dim tableResults As DataTable = Nothing
    8.         Dim rowResults As New List(Of DataRow)()
    9.         Dim names As New List(Of String)()
    10.  
    11.         'Create structure
    12.         With table
    13.             With .Columns
    14.                 .Add("ID", GetType(Integer))
    15.                 .Add("NAME", GetType(String))
    16.                 .Add("DATE", GetType(Date))
    17.                 .Add("JOB", GetType(String))
    18.             End With
    19.             With .Rows
    20.                 .Add(0, "Joe", #1/1/2010#, "Selling")
    21.                 .Add(1, "Bob", #1/1/2010#, "Buying")
    22.                 .Add(2, "Joe", #2/1/2010#, "Manager")
    23.                 .Add(3, "John", #1/1/2010#, "Buying")
    24.                 .Add(4, "John", #1/3/2010#, "Buying")
    25.             End With
    26.         End With
    27.  
    28.         'Clone table structure
    29.         tableResults = table.Clone()
    30.  
    31.         'Get unique names
    32.         For Each row As DataRow In table.Rows
    33.             If Not names.Contains(row("NAME").ToString()) Then
    34.                 names.Add(row("NAME").ToString())
    35.             End If
    36.         Next
    37.  
    38.         'Get latest entry
    39.         For Each name As String In names
    40.             Dim maxDate As Date = Date.MinValue
    41.             Dim maxDateRow As DataRow = Nothing
    42.             table.DefaultView.RowFilter = String.Format("[NAME] LIKE '{0}'", name)
    43.             For Each row As DataRowView In table.DefaultView
    44.                 Dim [date] As Date = CDate(row("DATE"))
    45.                 If [date] > maxDate Then
    46.                     maxDate = [date]
    47.                     maxDateRow = row.Row
    48.                 End If
    49.             Next
    50.             rowResults.Add(maxDateRow)
    51.         Next
    52.  
    53.         'Add results to a new DataTable
    54.         For Each row As DataRow In rowResults
    55.             Dim newRow As DataRow = tableResults.NewRow()
    56.             newRow.ItemArray = row.ItemArray
    57.             tableResults.Rows.Add(newRow)
    58.         Next
    59.  
    60.         'Set DataSource
    61.         Me.DataGridView1.DataSource = tableResults
    62.  
    63.     End Sub
    64.  
    65. End Class

  10. #10

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Sorting a datatable out?

    This is brilliant! It works!

    I need to work this out in my head now....
    If you find my thread helpful, please remember to rate me

  11. #11

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: [RESOLVED] Sorting a datatable out?

    Is there a way of doing this in SQL query?
    If you find my thread helpful, please remember to rate me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width