I am trying to use the LINQ query to determine how many of each particular type of object I have and write these values to an anonymous type.
Let's say I have some data that looks like this (actually there are objects subject to this property, but they will work the same way)
GroupId 1 1 2 2 2 3
I know how to format my query in SQL. It will be something like this:
SELECT grp = GroupId, cnt = COUNT(*) FROM myTable GROUP BY GroupId
In this case, the output will look like this :
GroupID Count 1 2 2 3 3 1
How can I do the same with LINQ in vb.net
Dim groupCounts = From person In data Group By person.GroupId Select new {group = person.GroupId, count = count(*)}
This is not entirely true, but I think it is close.
Also, without knowing much about anonymous types, can I actually declare groupCounts earlier that it will be an enumeration of elements, each of which has a group and counter property?
source share