Skip to content

Instantly share code, notes, and snippets.

@petterek
Created May 8, 2012 11:14
Show Gist options
  • Save petterek/2634299 to your computer and use it in GitHub Desktop.
Save petterek/2634299 to your computer and use it in GitHub Desktop.
Transform and do an action on a enurable(Of )
Public Class Transformers
Public Shared Function OptimusPrime(Of T, TT As {New})(incol As IEnumerable(Of T), comp As Comparer(Of T, TT), action As Action(Of TT, T)) As IEnumerable(Of TT)
Dim ret As New List(Of TT)
For Each p In incol
Dim curP = p
Dim cur = ret.Find(Function(o) comp(curP, o))
If cur Is Nothing Then
cur = New TT
ret.Add(cur)
End If
action(cur, p)
Next
Return ret
End Function
Public Delegate Function Comparer(Of T, TT)(o As T, o2 As TT) As Boolean
End Class
'Use:
Dim res = Utils.Transformers.OptimusPrime(Of UserProfilePoint, UserProfileScore)(ret,
Function(o1, o2)
Return o1.UserprofileId = o2.UserProfileId
End Function,
Sub(o1, o2)
o1.UserProfileId = o2.UserprofileId
o1.Year = o2.Earned.Year
o1.Month = o2.Earned.Month
o1.Points += o2.Points
End Sub)
res.OrderBy(Function(o) o.Points)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment