documentation有一个关于对多个属性进行分组的示例:
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)
,或者是否必须使用匿名类型,并对第一个查询的结果运行第二个查询?
答案 0 :(得分:0)
事实证明这比我试过的一些事情要简单得多,因为我并不欣赏你能做到的事情:
Dim custRegionQuery = From cust In db.Customers _
Group By cust.City, cust.Region _
Into Group _
Select New CustomerRegionGroup(City, Region, Group)