我总是在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
答案 0 :(得分: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)
答案 1 :(得分: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