2

我有以下 LINQ 代码,它有语法错误,我不知道如何修复它

         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

那我做错了什么?

这是表格的样子,我想按 cprice 和 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 |      |
+---------------+---------------+-----------+------+
4

1 回答 1

5

试试这个:

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

使用Key修饰符,这将创建一个匿名类型,该类型基于其成员的相等性来实现相等。

于 2012-09-18T18:53:55.833 回答