2012-09-18 74 views
2

我有有語法錯誤以下LINQ代碼和我不知道如何解決它VB.NET 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 |  | 
+---------------+---------------+-----------+------+ 
+0

什麼語法錯誤? –

+0

@GertArnold阿諾德我得到「類型與預期」 – user1570048

回答

4

組試試這個:

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

Uisng的Key修改,這將創建一個匿名類型根據equality of its members實現平等。

+1

謝謝,第一個選項的工作,但我沒有得到我期望的結果,我期望產品具有相同的cprice和相同的ccategory在一個組中,所以我有5800行,它給了我5800組! – user1570048

+0

意外。編輯我的答案。 –

+0

現在它可以工作,謝謝! :D – user1570048