How to find which time value is the oldest? - vb.net

In my project, at some point I tend to load all rows in my DataSet and add them to my FlowLayoutPanel in order of their "Time" Item. This is what I'm trying to do but I need the controls to be added first if their "Time" value are the oldest :
For i=1 to DataSet.Tables("myTable").Rows.Count
Dim Row as Datarow = DataSet.Tables("myTable").Select("ID = " & i)
Dim Time as Date = Row.Item("Time")
Dim NewLabel as Label
NewLabel.text = Time.ToString()
FlowLayoutPanel.Controls.Add(newLabel)
Next
How do I do that ?

You can use Linq-To-DataSet:
Dim timeOrderedRows = From row in DataSet.Tables("myTable")
Order By row.Field(Of Date)("Time")
For Each row As DataRow In timeOrderedRows
Dim Time as Date = row.Field(Of Date)("Time")
Dim Label as New Label
Label.text = Time.ToString()
FlowLayoutPanel.Controls.Add(Label)
Next
How can I do this in reverse (from newest to the oldest)
You just have to add Descending:
....
Order By row.Field(Of Date)("Time") Descending

Related

Search and replace inside string column in DataTable is slow?

I am fetching distinct words in a string column of a DataTable (.dt) and then replacing the unique values with another value, so essentially changing words to other words. Both approaches listed below work, however, for 90k records, the process is not very fast. Is there a way to speed up either approach?
The first approach, is as follows:
'fldNo is column number in dt
For Each Word As String In DistinctWordList
Dim myRow() As DataRow
myRow = dt.Select(MyColumnName & "='" & Word & "'")
For Each row In myRow
row(fldNo) = dicNewWords(Word)
Next
Next
A second LINQ-based approach is as follows, and is actually not very fast either:
Dim flds as new List(of String)
flds.Add(myColumnName)
For Each Word As String In DistinctWordsList
Dim rowData() As DataRow = dt.AsEnumerable().Where(Function(f) flds.Where(Function(el) f(el) IsNot DBNull.Value AndAlso f(el).ToString = Word).Count = flds.Count).ToArray
ReDim foundrecs(rowData.Count)
Cnt = 0
For Each row As DataRow In rowData
Dim Index As Integer = dt.Rows.IndexOf(row)
foundrecs(Cnt) = Index + 1 'row.RowId
Cnt += 1
Next
For i = 0 To Cnt
dt(foundrecs(i))(fldNo) = dicNewWords(Word)
Next
Next
So you have your dictionary of replacements:
Dim d as New Dictionary(Of String, String)
d("foo") = "bar"
d("baz") = "buf"
You can apply them to your table's ReplaceMe column:
Dim rep as String = Nothing
For Each r as DataRow In dt.Rows
If d.TryGetValue(r.Field(Of String)("ReplaceMe"), rep) Then r("ReplaceMe") = rep
Next r
On my machine it takes 340ms for 1 million replacements. I can cut that down to 260ms by using column number rather than name - If d.TryGetValue(r.Field(Of String)(0), rep) Then r(0) = rep
Timing:
'setup, fill a dict with string replacements like "1" -> "11", "7" -> "17"
Dim d As New Dictionary(Of String, String)
For i = 0 To 9
d(i.ToString()) = (i + 10).ToString()
Next
'put a million rows in a datatable, randomly assign dictionary keys as row values
Dim dt As New DataTable
dt.Columns.Add("ReplaceMe")
Dim r As New Random()
Dim k = d.Keys.ToArray()
For i = 1 To 1000000
dt.Rows.Add(k(r.Next(k.Length)))
Next
'what range of values do we have in our dt?
Dim minToMaxBefore = dt.Rows.Cast(Of DataRow).Min(Function(ro) ro.Field(Of String)("ReplaceMe")) & " - " & dt.Rows.Cast(Of DataRow).Max(Function(ro) ro.Field(Of String)("ReplaceMe"))
'it's a crappy way to time, but it'll prove the point
Dim start = DateTime.Now
Dim rep As String = Nothing
For Each ro As DataRow In dt.Rows
If d.TryGetValue(ro.Field(Of String)("ReplaceMe"), rep) Then ro("ReplaceMe") = rep
Next
Dim ennd = DateTime.Now
'what range of values do we have now
Dim minToMaxAfter = dt.Rows.Cast(Of DataRow).Min(Function(ro) ro.Field(Of String)("ReplaceMe")) & " - " & dt.Rows.Cast(Of DataRow).Max(Function(ro) ro.Field(Of String)("ReplaceMe"))
MessageBox.Show($"min to max before of {minToMaxBefore} became {minToMaxAfter} proving replacements occurred, it took {(ennd - start).TotalMilliseconds} ms for 1 million replacements")

Adding row in DataTable

I'm doing a project in vb and I'm trying to add all the rows in dataTable name dt to a new dataTable name dtNew but i dont want duplicate rows to be added in dtNew instead adding the count if its duplicate. someone help me please.
here is a sample dataTable name dt as you can see apple is duplicate
here is my code.
Dim dtNew As DataTable = New DataTable '---> I Created new DataTable
'---> Created 3 columns
dtNew.Columns.Add("TYPE", Type.GetType("System.String"))
dtNew.Columns.Add("NAME", Type.GetType("System.String"))
dtNew.Columns.Add("COUNT", Type.GetType("System.Int32"))
For Each dtRow As DataRow In dt.Rows ' ---> loop all the rows in dt DataTable
Dim newRow As DataRow = dtNew.NewRow
newRow("TYPE") = dtRow("TYPE")
newRow("NAME") = dtRow("NAME")
newRow("COUNT") = dtRow("COUNT")
'check if dtNew DataTable has no row
If Not dtNew Is Nothing AndAlso dtNew.Rows.Count = 0 Then
'add new row
dtNew.Rows.Add(newRow)
Else
' I want to check first all the rows in dtNew DataTable
' if its existed, and if it's not then add new row
For Each dtNewRow As DataRow In dtNew.Rows
If ((dtNewRow("TYPE") = "VEGETABLE" OrElse _
dtNewRow("TYPE") = "FRUIT") And _
dtNewRow("NAME") <> newRow("NAME")) Then
'insert row
dtNew.Rows.InsertAt(newRow, dtNew.Rows.Count)
'error: Collection was modified; enumeration operation might not be executed.
End If
Next
End If
Next
For Each row As DataRow In dt.Rows
Dim type = CStr(row("Type"))
Dim name = CStr(row("Name"))
Dim existingRows = dtNew.Select(String.Format("Type = '{0}' AND Name = '{1}'",
type,
name))
If existingRows.Length = 0 Then
'No match so create a new row.
Dim newRow = dtNew.NewRow()
newRow("Type") = type
newRow("Name") = name
newRow("Count") = row("Count")
dtNew.Rows.Add(newRow)
Else
'Match found so update existing row.
Dim newRow = existingRows(0)
newRow("Count") = CInt(newRow("Count")) + CInt(row("Count"))
End If
Next
As the tables have the same schema, you could even simplify the If block to this:
dtNew.ImportRow(row)
That would only be a problem if row had a RowState other than Unchanged and you didn't want to import that too.

Distinct Rows From Vb.Net DataTable

I have a scenario, in which I have to apply distinct filter onto DataTable and find the rows only which are distinct,
I am using dt.DefaultView.ToTable(True, Columns) this statement but no effect.
Here is my chunk of code..
Try
Dim dTable As New DataTable()
dTable.Columns.Add("AutoID")
dTable.Columns.Add("AnotherID")
dTable.Columns.Add("CitY")
Dim row As DataRow = Nothing
For i As Integer = 0 To 4
row = dTable.NewRow()
row("AutoID") = i + 1
row("AnotherID") = i + 10
row("City") = "Vetican"
dTable.Rows.Add(row)
Next
dTable.Rows.Add(6, "11", "Oslo")
dTable.Rows.Add(7, "12", "Toronto")
Dim TobeDistinct As String() = {"AnotherID"}
Dim dtDistinct As DataTable = GetDistinctRecords(dTable, TobeDistinct)
Catch ex As Exception
End Try
and the method ..
Public Shared Function GetDistinctRecords(ByVal dt As DataTable, ByVal Columns As String()) As DataTable
Dim dtURecords As New DataTable()
dtURecords = dt.DefaultView.ToTable(True, Columns)
Return dtURecords
End Function
Here is the screen shot , which I want..
Which rows do you want to keep and which rows should be removed? If you just want to keep one row per AnotherID it seems to be arbitrary to keep Vetican instead of Oslo. Maybe you want to concat both as in Vetican, Oslo.
I would use Linq instead:
Dim resultTable = dTable.Clone() ' empty table same columns
Dim idGroups = dTable.AsEnumerable().GroupBy(Function(r) r.Field(Of String)("AnotherID"))
For Each grp In idGroups
Dim r As DataRow = resultTable.Rows.Add()
r.SetField("AutoID", grp.First().Field(Of String)("AutoID"))
r.SetField("AnotherID", grp.Key)
Dim cities = From row In grp Select row.Field(Of String)("City")
r.SetField("City", String.Join(", ", cities))
Next

how to search DataTable for specific record?

Hi,
I have a windows form with 10 text fields and 1 combobox.
When the user selects a record in the combo-box I want to find that record in my form datatable variable (called dtBranches) then populate my 10 textfields from the datarow.
I tried this:
Dim dr As System.Data.DataRow
If mSortCode > 0 Then
dr = dtBranches.Select("SortCode='" & mSortCode & "'")
Me.txtBranch.Text = dr("Branch").ToString()
Me.txtBankName.Text = dr("BankName").ToString()
Me.txtBranchTitle.Text = dr("BranchTitle").ToString()
Me.txtReference.Text = dr("Ref").ToString
Me.txtAddr1.Text = dr("Address1").ToString
Me.txtAddr2.Text = dr("Address2").ToString
Me.txtAddr3.Text = dr("Address3").ToString
Me.txtPostCode.Text = dr("PostCode").ToString
Me.txtTelNo.Text = dr("TelephoneNumber").ToString
Me.txtTown.Text = dr("Town").ToString
Me.txtTelNo.Text = dr("TelephoneNumber").ToString
end if
but can't get it to compile...
What's the correct and best way to do this please?
thanks
Philip
DataTable.Select returns an array of DataRows. You need to declare an array to receive the result
Dim dr() As System.Data.DataRow
Of course then you need to check if you have rows returned and address the first row in the array
dr = dtBranches.Select("SortCode='" & mSortCode & "'")
If dr.Length > 0 Then
Me.txtBranch.Text = dr(0)("Branch").ToString()
Me.txtBankName.Text = dr(0)("BankName").ToString()
...... and so on ...
I would use Linq-ToDataSet and the strongly typed Field method instead:
Dim matches = From row In dtBranches
Let SortCode = row.Field(Of String)("SortCode")
Where SortCode = mSortCode
If matches.Any() Then
Dim row = matches.First()
Me.txtBranch.Text = row.Field(Of String)("Branch")
Me.txtBankName.Text = row.Field(Of String)("BankName")
Me.txtBranchTitle.Text = row.Field(Of String)("BranchTitle")
Me.txtReference.Text = row.Field(Of String)("Ref")
Me.txtAddr1.Text = row.Field(Of String)("Address1")
Me.txtAddr2.Text = row.Field(Of String)("Address2")
Me.txtAddr3.Text = row.Field(Of String)("Address3")
Me.txtPostCode.Text = row.Field(Of String)("PostCode")
Me.txtTelNo.Text = row.Field(Of String)("TelephoneNumber")
Me.txtTown.Text = row.Field(Of String)("Town")
Else
MesageBox.Show("SortCode not found.")
End If
If you want to compare case-insensitively, replace the Where above with:
Where StringComparer.OrdinalIgnoreCase.Equals(SortCode, mSortCode)
By the way, you are assigning the telephone number twice.

How to find the original position in a DataTable from a filtered DataView

I want to find a particular ID in a DataTable, I am using a DataView to filter the results, but how do I know which row in the original table the filter view corresponds to? Any ideas? In know I could use LINQ but I don't think this would help either?
Dim dt As New DataTable
Dim dv As New DataView(dt)
dv.RowFilter = "ID = 123"
If dv.Count = 1 Then
'which datarow in the original datatable is this?
End If
EDIT
I want to avoid having to loop through the DataTable to find this:
For r As Integer = 0 To dt.Rows.Count - 1
If CInt(dt.Rows(r).Item("ID")) = 123 Then
Debug.WriteLine("Found it at row " + r.ToString)
Exit For
End If
Next
Nothing could be easier:
Dim dr As DataRow = dv(0).Row
http://msdn.microsoft.com/en-us/library/system.data.datarowview.row%28v=VS.100%29.aspx
Edit: According to your comment, you could use following to get the index of the row
Dim rowIndex As Int32 = -1
For i As Int32 = 0 To dr.Table.Rows.Count - 1
If dr.Table.Rows(i)("ID").Equals(dr("ID")) Then
rowIndex = i
Exit For
End If
Next
Or in this shorter way:
Dim rowIndex As Int32 = dr.Table.Rows.IndexOf(dr)
http://msdn.microsoft.com/en-us/library/system.data.datarowcollection.indexof.aspx