|
-
Jan 25th, 2010, 04:15 PM
#1
Thread Starter
Frenzied Member
[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 
-
Jan 25th, 2010, 04:19 PM
#2
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?
-
Jan 25th, 2010, 04:37 PM
#3
Thread Starter
Frenzied Member
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 
-
Jan 25th, 2010, 05:01 PM
#4
Re: Sorting a datatable out?
Latest entry based on date right?
vb.net Code:
Public Class Form1 Public Sub New() Me.InitializeComponent() Dim table As New DataTable() With table With .Columns .Add("ID", GetType(Integer)) .Add("Name", GetType(String)) .Add("Date", GetType(Date)) .Add("Job", GetType(String)) End With With .Rows .Add(0, "Joe", #1/1/2010#, "Selling") .Add(1, "Bob", #1/1/2010#, "Buying") .Add(2, "Joe", #2/1/2010#, "Manager") .Add(3, "John", #1/1/2010#, "Buying") End With End With Dim results = table.AsEnumerable().OrderBy(Function(row) row.Field(Of Date)("Date")) _ .GroupBy(Function(row) row.Field(Of String)("Name")) _ .Select(Function(grouping) grouping.Last()).CopyToDataTable() Me.DataGridView1.DataSource = results End Sub End Class
-
Jan 25th, 2010, 05:06 PM
#5
Re: Sorting a datatable out?
Ahhhh just saw that it was VB2005!! Disregard my post...
-
Jan 25th, 2010, 05:15 PM
#6
Thread Starter
Frenzied Member
Re: Sorting a datatable out?
Bugger, can it not be done in VS2005?
If you find my thread helpful, please remember to rate me 
-
Jan 25th, 2010, 05:23 PM
#7
Re: Sorting a datatable out?
 Originally Posted by dinosaur_uk
Bugger, can it not be done in VS2005?

Not with LINQ... Upgrade? 
Kidding, I'll think about the logic for VB2005.
-
Jan 25th, 2010, 06:01 PM
#8
Thread Starter
Frenzied Member
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 
-
Jan 25th, 2010, 06:28 PM
#9
Re: Sorting a datatable out?
 Originally Posted by dinosaur_uk
Thanks dude,
my code at the moment is crazily long....and it still doesnt work!
Try this:
vb.net Code:
Public Class Form1
Public Sub New()
Me.InitializeComponent()
Dim table As New DataTable()
Dim tableResults As DataTable = Nothing
Dim rowResults As New List(Of DataRow)()
Dim names As New List(Of String)()
'Create structure
With table
With .Columns
.Add("ID", GetType(Integer))
.Add("NAME", GetType(String))
.Add("DATE", GetType(Date))
.Add("JOB", GetType(String))
End With
With .Rows
.Add(0, "Joe", #1/1/2010#, "Selling")
.Add(1, "Bob", #1/1/2010#, "Buying")
.Add(2, "Joe", #2/1/2010#, "Manager")
.Add(3, "John", #1/1/2010#, "Buying")
.Add(4, "John", #1/3/2010#, "Buying")
End With
End With
'Clone table structure
tableResults = table.Clone()
'Get unique names
For Each row As DataRow In table.Rows
If Not names.Contains(row("NAME").ToString()) Then
names.Add(row("NAME").ToString())
End If
Next
'Get latest entry
For Each name As String In names
Dim maxDate As Date = Date.MinValue
Dim maxDateRow As DataRow = Nothing
table.DefaultView.RowFilter = String.Format("[NAME] LIKE '{0}'", name)
For Each row As DataRowView In table.DefaultView
Dim [date] As Date = CDate(row("DATE"))
If [date] > maxDate Then
maxDate = [date]
maxDateRow = row.Row
End If
Next
rowResults.Add(maxDateRow)
Next
'Add results to a new DataTable
For Each row As DataRow In rowResults
Dim newRow As DataRow = tableResults.NewRow()
newRow.ItemArray = row.ItemArray
tableResults.Rows.Add(newRow)
Next
'Set DataSource
Me.DataGridView1.DataSource = tableResults
End Sub
End Class
-
Jan 25th, 2010, 07:53 PM
#10
Thread Starter
Frenzied Member
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 
-
Jan 26th, 2010, 08:45 AM
#11
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|