该文档有一个关于对多个属性进行分组的示例:
Dim custRegionQuery = From cust In db.Customers _
Group cust.ContactName By Key = New With _
{cust.City, cust.Region} Into grouping = Group
For Each grp In custRegionQuery
Console.WriteLine(vbNewLine & "Location Key: {0}", grp.Key)
For Each listing In grp.grouping
Console.WriteLine(vbTab & "{0}", listing)
Next
Next
假设我有一个真正的类型(即不是匿名的),它具有键的每个元素的属性,以及组的属性,所以有点像:
Public Class CustomerRegionGroup
Public Sub New(city As String, region as String, customers As IEnumerable(Of Customer))
Me.City = city
Me.Region = region
Me.Customers = customers
End Sub
Public Property City As String
Public Property Region As String
Public Property Customers As IEnumerable(Of Customer)
End Class
是否可以将原始查询重写为仅返回IEnumerable(Of CustomerRegionGroup)
,或者我是否必须使用匿名类型,并对第一个查询的结果运行第二个查询?