2017-04-16 70 views
0

在VB.NET中工作,並假設它將像C#一樣工作。顯然是愚蠢的。分組通過匿名類型比較的參考不屬性

dim moqValidationData = new DataTable() 

moqValidationData.Columns.Add("TransactionItemID") 
moqValidationData.Columns("TransactionItemID").dataType = GetType(Integer) 
moqValidationData.Columns.Add("AdjustmentType") 
moqValidationData.Columns("AdjustmentType").dataType = GetType(String) 
moqValidationData.Columns.Add("DeliveryDate") 
moqValidationData.Columns("DeliveryDate").dataType = GetType(Date) 
moqValidationData.Columns.Add("AdjustmentAmount") 
moqValidationData.Columns("AdjustmentAmount").dataType = GetType(Double) 

moqValidationData.Rows.Add(1,"Set To Zero",Nothing,Nothing) 
moqValidationData.Rows.Add(2,"Adjustment Value",New Date(2017,1,1),1) 
moqValidationData.Rows.Add(2,"Adjustment Value",New Date(2017,1,2),2) 
moqValidationData.Rows.Add(2,"Adjustment Value",New Date(2017,1,3),3) 

dim raw = moqValidationData.Rows.Cast(Of DataRow) 

試過這種

dim obj1 = 
    raw.GroupBy(Function (g) new With { .TiID = CInt(g("TransactionItemId")), g("AdjustmentType").ToString()}, 
       Function(key,grp) new With{ .Ti=key.TiID, .adj = key.Adj, .vals = grp.ToList() }). 

dim obj2 = 
    raw.GroupBy(Function (g) new With { .TiID = CInt(g("TransactionItemId")), .Adj = g("AdjustmentType").ToString()}). 
     Select(Function (s) new With {.Key = s.Key, .Vals = s.ToList()}).ToList() 

我相信OBJ1/2的關係的2項與」 .vals名單上的每一個都是DataRow收集的1 3.相反,我總是在每個項目的「.vals」集合中使用單個「DataRow」獲取四個列表。基於之類的東西

  1. Why anonymous types Equals implementation compares fields?
  2. Group By Multiple Columns

我的期望似乎是正確的。我錯過了什麼?

+0

在VB中,你必須NET應用'您要成爲平等比較的一部分匿名類型屬性Key'關鍵字。 –

+0

@IvanStoev就是這樣,謝謝! – Kludge

回答

0

根據記錄,添加關鍵字「關鍵」的樣子:

dim objs = raw.GroupBy(Function (g) new With { 
    Key.TiID = CInt(g("TransactionItemId")), 
    Key.Adj = g("AdjustmentType").ToString()}). 
      Select(Function (s) new With {.Key = s.Key, .Vals = s.ToList()}).ToList()