[C#] Programming Challenge With LINQ

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
Can anybody tell me what my code does below? :bored2:

Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
	Dim ListGroups As IEnumerable(Of IGrouping(Of String, ListViewItem)) _
		= ListView1.Items.Cast(Of ListViewItem).GroupBy(Function(Obj) Obj.Text)

	Dim LVIs As ListViewItem() = (From IG As IGrouping(Of String, ListViewItem) In ListGroups
		Let posVal As Integer = ReturnTotal(IG, True)
		Let negVal As Integer = ReturnTotal(IG, False)
		Select New ListViewItem(New String() {IG.Key, CStr(posVal - negVal), ""})).ToArray

	ListView2.Items.AddRange(LVIs)
End Sub

Private Function ReturnTotal(IG As IGrouping(Of String, ListViewItem), ReturnPositives As Boolean) As Integer
	Dim ItemIsPositive As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Green
	Dim ItemIsNegative As Func(Of ListViewItem, Boolean) = Function(LVI) LVI.ForeColor = Color.Red

	Return If(ReturnPositives, IG.Where(ItemIsPositive), IG.Where(ItemIsNegative)).Select(Function(x) Integer.Parse(x.SubItems(1).Text)).Aggregate(Function(a, b) a + b)
End Function

Points of self satisfaction awarded to those who can tell me what it does preferably without debugging. Only thing you can use (and this should be plenty sufficient for trying to provide me with an answer, even if you're not a programmer), would be MSDN documentation: http://msdn.microsoft.com/en-us/library/gg145045

Search MSDN for the methods i'm using. It's easy enough to do that. You can use the Bing search to search MSDN on that link i've provided :)

If you debug my code to find out what it does, it still takes some ability to read the code to make sure you're using it in the correct way, so i'll still give you credits if you debug it.

Those of you who cheat (because I have posted this in reply to help someone on VBForums), well... You may still come here with the answer, but the self satisfaction may not be present :)

Good luck
~Ace
 
Last edited:

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top