This updated version uses a subroutine to do the same thing as before, but doesn't need the extra internal field. (I have kept my earlier version, which, to avoid using a Zip
routine, needed the extra OrDer
field.)
Option Explicit On
Option Strict On
Option Infer On
Imports so11310237.JobTypes
Module so11310237
Enum JobTypes
JobType1
JobType2
JobType3
End Enum
Sub Main()
Dim data = {New With{.CO=#11:00#, .JT=JobType1, .OD=0},
New With{.CO=#11:03#, .JT=JobType2, .OD=0},
New With{.CO=#11:05#, .JT=JobType3, .OD=0},
New With{.CO=#11:02#, .JT=JobType2, .OD=0},
New With{.CO=#11:06#, .JT=JobType1, .OD=0},
New With{.CO=#11:01#, .JT=JobType2, .OD=0},
New With{.CO=#11:04#, .JT=JobType2, .OD=0},
New With{.CO=#11:07#, .JT=JobType1, .OD=0}}
' Check that there's any data to process
If Not data.Any Then Exit Sub
' Both versions include a normal ordering first.
Dim odata = From q In data Order By q.CO
' First version here (and variables reused below):
Dim ljt = odata.First.JT
Dim c = 0
For Each o In odata
If ljt <> o.JT Then
ljt = o.JT
c += 1
End If
o.OD = c
Next
For Each p In From q In data Group By r=q.JT, d=q.OD Into Count()
Console.WriteLine(p)
Next
Console.WriteLine()
' New version from here:
' Reset variables (still needed :-()
ljt = odata.First.JT
c = 0
For Each p In From q In odata Group By r=q.JT, d=IncIfNotEqual(c,q.JT,ljt) Into Count()
Console.WriteLine(p)
Next
End Sub
Function IncIfNotEqual(Of T)(ByRef c As Integer, ByVal Value As T, ByRef Cmp As T) As Integer
If Not Object.Equals(Value, Cmp) Then
Cmp = Value
c += 1
End If
Return c
End Function
End Module