我不知道我的问题标题对我的情况是否正确。以下是我的问题。 以下是我的课程。
Public Class Employee
public Name As String
public ID as String
public SaleDivs as List(of SalesDivision)
End Class
Public Class SalesDivision
public Name As String
public ID as String
public SaleLocations as List(of SaleLocation)
End Class
Public Class SaleLocation
public Name As String
public ID as String
End Class
我有Employess
的列表。在该列表中,可以将销售地点分配给多个员工,并且员工可以拥有多个销售地点。
我如何使用Linq找到为特定销售地点分配的员工数量。
我正在使用.NET CE Framework 3.5。
提前致谢。
答案 0 :(得分:2)
您可以使用多个From
子句(a.k.a。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
。