0

我一直在努力在 LINQ 中进行分组,我想根据日期时间字段的日期部分进行分组并聚合其他字段。

示例表布局

myDateField     myIntField1    myIntField2    myIntField3
01/02/2013      5              5              5
01/02/2013      5              5              5
02/02/2013      10             10             10
02/02/2013      10             10             10

我想返回一个列表

myDateField     myIntField1    myIntField2    myIntField3
01/02/2013      10             10             10
02/02/2013      20             20             20

我已经使用 msdn 示例管理了以下内容,但无法真正了解如何使用它。它指出我应该能够访问新BookedList组,但不断收到匿名类型错误等。

        Dim bestdays = From a In b
                        Where a.BookedOn < a.EventDayTime And a.Cancelled = False
                        Group By BookDate = a.BookedOn.Value.Date
                        Into BookedList = Group
4

2 回答 2

1

语法是Into [FIELD_NAME1] = [AGGREGATE_FUNCTION1]([VALUE1]), [FIELD_NAME2] = [AGGREGATE_FUNCTION2]([VALUE2]) .... 密钥将自动成为返回对象的一部分。

所以你正在寻找这样的东西:

Class TableRow
  Public Property myDateField As Date
  Public Property myIntField1 As Integer
  Public Property myIntField2 As Integer
  Public Property myIntField3 As Integer
  Sub New(mydate As Date,
          myint1 As Integer, myint2 As Integer, myint3 As Integer)
    myDateField = mydate
    myIntField1 = myint1
    myIntField2 = myint2
    myIntField3 = myint3
  End Sub
End Class

Sub Main()
  Dim tablesRows As New List(Of TableRow)
  tablesRows.Add(New TableRow(Today, 5, 10, 15))
  tablesRows.Add(New TableRow(Today, 6, 11, 16))

  Dim v = From r In tablesRows Group By r.myDateField
          Into Total1 = Sum(r.myIntField1),
               Total2 = Sum(r.myIntField2),
               Total3 = Sum(r.myIntField3)
End Sub
于 2013-05-14T13:53:03.437 回答
1

也许这可以做你的工作

Dim bestdays = (From a In b
               Where a.BookedOn < a.EventDayTime And a.Cancelled = False
               Group By BookDate = a.BookedOn.Value.Date
               Into BookedList = Group).Take(5)
于 2013-05-14T13:31:32.560 回答