user1570048 user1570048 - 5 months ago 35
Vb.net Question

VB.NET LINQ Group By Multiple Columns

I have the following LINQ code which has a syntax error and i've no idea how to fix it

Dim query =
From row In mainDatatable.AsEnumerable

'the syntax Error is in the following line
Group row By new { row.Field(Of Double)("cprice") , row.Field(Of Integer)("ccategory")}
Into ProductGroups
'**************************

Dim grpNumber = 1
For Each grp In query
For Each row In grp.ProductGroups
row.SetField("gnum", grpNumber)
Next
grpNumber += 1
Next


End Sub


so what am i doing wrong?

Here is how the table looks like and i want to group by the columns cprice and ccategory

+---------------+---------------+-----------+------+
| Product Name | cprice | ccategory | gnum |
+---------------+---------------+-----------+------+
| Skirt Red | 99 | 1 | |
| Jeans Blue | 49 | 2 | |
| Jeans Black | 49 | 2 | |
| Skirt Blue | 99 | 1 | |
| T-shirt White | 20 | 2 | |
| T-shirt Green | 20 | 2 | |
| Jeans Grey | 49 | 2 | |
+---------------+---------------+-----------+------+

Answer

Try this:

new with { Key.Price = row.Field(Of Double)("cprice") ,
           Key.Category = row.Field(Of Integer)("ccategory")

Uisng the Key modifier, this creates an anonymous type that implements equality based on equality of its members.