1

文档有一个关于对多个属性进行分组的示例:

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),或者我是否必须使用匿名类型,并对第一个查询的结果运行第二个查询?

4

1 回答 1

0

事实证明它比我尝试过的一些事情要简单得多,因为我不明白你可以这样做:

Dim custRegionQuery = From cust In db.Customers _
                      Group By cust.City, cust.Region _
                      Into Group _
                      Select New CustomerRegionGroup(City, Region, Group)
于 2012-04-24T11:03:39.700 回答