Group By匿名类型是按ref而不是属性进行比较

时间:2017-04-16 15:47:23

标签: vb.net linq

在VB.NET中工作并假设它将像C#一样工作。显然是愚蠢的。

java.lang.IndexOutOfBoundsException

试过这个

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() } ).

我相信obj1 / 2应该是2个项目的列表,每个项目都是{。1}}的1个和3个集合。相反,我总是得到四个列表,每个列表中有一个'DataRow' items'.vals'系列。

基于以下内容:

  1. Why anonymous types Equals implementation compares fields?
  2. Group By Multiple Columns
  3. 我的期望似乎是正确的。我错过了什么?

1 个答案:

答案 0 :(得分:0)

对于记录,添加关键字'Key'如下:

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()