您可以使用多个From
子句(aka SelectMany
)。
给定以下示例数据:
Dim loc1 = New SaleLocation With { .Name = "Location1" }
Dim loc2 = New SaleLocation With { .Name = "Location2" }
Dim div1 = New SalesDivision With { .Name = "Div1", .SaleLocations = New List(Of SaleLocation) From { loc1 }}
Dim div2 = New SalesDivision With { .Name = "Div2", .SaleLocations = New List(Of SaleLocation) From { loc2 }}
Dim div3 = New SalesDivision With { .Name = "Div3", .SaleLocations = New List(Of SaleLocation) From { loc1, loc2 }}
Dim emp1 = New Employee With { .Name = "Harry", .SaleDivs = New List(Of SalesDivision) From { div1 }}
Dim emp2 = New Employee With { .Name = "Marry", .SaleDivs = New List(Of SalesDivision) From { div2 }}
Dim emp3 = New Employee With { .Name = "Larry", .SaleDivs = New List(Of SalesDivision) From { div3 }}
Dim employees = New List(Of Employee) From { emp1, emp2, emp3 }
您可以使用以下查询:
Dim result = From employee in employees
From division in employee.SaleDivs
From location in division.SaleLocations
Where location.Name = "Location2"
Select employee Distinct
并得到计数:
Dim numberOfLocation2Employees = result.Count()
在您的真实代码中,您可能使用该ID
字段而不是Name
.