2013-10-03 42 views
0

使用子列表中的項目這是我的名單.NET選擇列表中的

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

我試過很多使用LINQ的組合,但它拋出空引用 :(讓我更新 – kbvishnu

+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))

+0

感謝您suggestion.I試過'Any'。我覺得3.5 CE不支持任何()。我有編譯問題,讓我再試一次。 – kbvishnu

+0

MSDN說它包含在3.5緊湊...也許我的VB.Net是不正確的(http://msdn.microsoft.com /en-us/library/bb534972(v=vs.90).aspx) – Moho

+0

是它包括在內,我正在努力.. – kbvishnu