.NET使用列表中的子列表选择项目

时间:2013-10-03 15:04:50

标签: .net vb.net linq

这是我的名单

locList.Add(New Location() With {.ID = "001", .Name = "KTYM"})
' 2 products
prdList.Add(New Product() With {.ID = "001", .Locations = locList, .Name = "Boost"})
prdList.Add(New Product() With {.ID = "003", .Locations = locList, .Name = "Pears"})


locList = New List(Of Location)
prdList = New List(Of Product)
locList.Add(New Location() With {.ID = "002", .Name = "TVM"})
' 3 products
prdList.Add(New Product() With {.ID = "002", .Locations = locList, .Name = "Horlicks"})
prdList.Add(New Product() With {.ID = "003", .Locations = locList, .Name = "Pears"})
prdList.Add(New Product() With {.ID = "004", .Locations = locList, .Name = "Colgate"})


locList = New List(Of Location)
prdList = New List(Of Product)
locList.Add(New Location() With {.ID = "003", .Name = "KNR"})
' 4 prod
prdList.Add(New Product() With {.ID = "B05", .Locations = locList, .Name = "CloseUP"})
prdList.Add(New Product() With {.ID = "003", .Locations = locList, .Name = "Pears"})
prdList.Add(New Product() With {.ID = "016", .Locations = locList, .Name = "Maggi"})
prdList.Add(New Product() With {.ID = "A07", .Locations = locList, .Name = "Dairy Milk"})

每个产品都有一个位置。我想从列表中选择给定位置的所有产品。我怎样才能实现它?我在CE设备上使用.net 3.5。谢谢你的帮助。

修改

Dim prd = From p As Product In prdList _
                  From loc As Location In p.Locations _
                  Where loc.ID = "001" _
                  Select p

这里prd.Count它返回零。

1 个答案:

答案 0 :(得分:1)

C#将是.Where( p => p.Locations.Any( loc => loc.ID == LocIdYouWant ) )

我认为这可能等同于VB.Net:

.Where( Function(p) p.Locations.Any( Function( loc ) loc.ID = LocIdYouWant ) )