mikeylikesit33
New member
- Joined
- Jan 26, 2015
- Messages
- 2
- Programming Experience
- 10+
Need some quick help as I've suffered over this easy problem all afternoon. Yes, I could do it in SQL easily then call that FillBy or 20 other different ways but I'm trying to figure out LINQ since I never used it before.
Group by Year Month and sum all three integer columns
Simple table
Date Integer1 Integer 2 Integer 3
2015/01/01 100 200 300
2015/01/02 101 201 301
2014/01/15 121 221 321
Expected output is
201501 201 401 601
201401 121 221 321
I've actually accomplished it a few times, but it's so messy I know it's stupid because of multiple steps that just don't look right. The best I've come up with is this
Dim dateGroups = From row In MyDataTable
Let year = row.Field(Of Date)("DATE").Year
Let month = row.Field(Of Date)("DATE").Month
Let Integer1 = row.Field(Of Int64)("Integer1")
Let Integer2 = row.Field(Of Int64)("Integer2")
Let Integer3 = row.Field(Of Int64)("Integer3")
Group row By Integer1, Integer2, Integer3, YearMonth = New With {Key .year = year, Key .month = month} Into DateGroup = Group Order By YearMonth.year, YearMonth.month
Dim sumGroups = From DateGroup In dateGroups
Group By DateGroup.YearMonth
Into sumInteger1 = Sum(CInt(DateGroup.Integer1)), sumInteger2 = Sum(CInt(DateGroup.Integer2)), sumInteger3 = Sum(CInt(DateGroup.Integer3))
It works! But as I said, it seems so ugly it can't be good. I should be able to do all the work in the first dim of dateGroups OR get rid of that and construct dateGroups a better way to feed my sumGroups. That first set of code does nothing more than replace my date field with a new Year and Month field. sigh...
Also, I expected I could then bind a datagrid to sumGroups but apparently not.
Can someone help an old fool who has been away from learning the new stuff too long and is desperately trying to teach myself? I seriously could do this 20 other ways in other languages or systems but this LINQ thing is baffling me for some reason.
Group by Year Month and sum all three integer columns
Simple table
Date Integer1 Integer 2 Integer 3
2015/01/01 100 200 300
2015/01/02 101 201 301
2014/01/15 121 221 321
Expected output is
201501 201 401 601
201401 121 221 321
I've actually accomplished it a few times, but it's so messy I know it's stupid because of multiple steps that just don't look right. The best I've come up with is this
Dim dateGroups = From row In MyDataTable
Let year = row.Field(Of Date)("DATE").Year
Let month = row.Field(Of Date)("DATE").Month
Let Integer1 = row.Field(Of Int64)("Integer1")
Let Integer2 = row.Field(Of Int64)("Integer2")
Let Integer3 = row.Field(Of Int64)("Integer3")
Group row By Integer1, Integer2, Integer3, YearMonth = New With {Key .year = year, Key .month = month} Into DateGroup = Group Order By YearMonth.year, YearMonth.month
Dim sumGroups = From DateGroup In dateGroups
Group By DateGroup.YearMonth
Into sumInteger1 = Sum(CInt(DateGroup.Integer1)), sumInteger2 = Sum(CInt(DateGroup.Integer2)), sumInteger3 = Sum(CInt(DateGroup.Integer3))
It works! But as I said, it seems so ugly it can't be good. I should be able to do all the work in the first dim of dateGroups OR get rid of that and construct dateGroups a better way to feed my sumGroups. That first set of code does nothing more than replace my date field with a new Year and Month field. sigh...
Also, I expected I could then bind a datagrid to sumGroups but apparently not.
Can someone help an old fool who has been away from learning the new stuff too long and is desperately trying to teach myself? I seriously could do this 20 other ways in other languages or systems but this LINQ thing is baffling me for some reason.