I have been playing around with grouping items using LINQ, and I am not sure if there is a difference between these two syntaxes:
Dim categories1 =
From p In products Group p By p.Category Into Group,
TotalUnits = Sum(p.UnitsInStock), ProductCount = Count()
Select Category, Average = TotalUnits / ProductCount
Dim categories2 =
From p In products Group p By p.Category Into
TotalUnits = Sum(p.UnitsInStock), ProductCount = Count()
Select Category, Average = TotalUnits / ProductCount
In the first, I do "Into Group," and in the second I do "Into". It seems that the "Group" in that case is a keyword rather than the name of the group. Both syntaxes appear to produce the same result. Is there a reason these two syntaxes exist (e.g., can one be used when the other cannot)? Is the second syntax just shorthand for the first?
Here is a class and function you can use to test the above queries, if you like:
Public Class ProductClass
Public Property Category As String
Public Property UnitsInStock As Integer
End Class
Public Function GetProductList() As List(Of ProductClass)
Return New List(Of ProductClass) From {
New ProductClass With {.Category = "A", .UnitsInStock = 5},
New ProductClass With {.Category = "A", .UnitsInStock = 3},
New ProductClass With {.Category = "B", .UnitsInStock = 4}}
End Function