2010-03-08 110 views
0

鑑於以下XML,我想返回所有eventtitles,其中eventtype id = 23。我當前的查詢只查看第一個eventtype,因此返回錯誤的結果。基於節點值的LINQ-to-XML選擇,新手問題

<event> 
     <eventtitle>Garrison Keillor</eventtitle> 
     <eventtypes> 
     <eventtype id="24"/> 
     <eventtype id="23"/> 
     </eventtypes> 
    </event> 
    <event> 
     <eventtitle>Joe Krown Trio featuring Walter Wolfman Washington</eventtitle> 
     <eventtypes> 
     <eventtype id="23"/> 
     </eventtypes> 
    </event> 

LINQ查詢:

Dim query = _ 
From c In calXML...<event> _ 
Where c...<eventtypes>.<eventtype>[email protected] = "23" _ 
Select c.<eventtitle>.Value, c.<eventlocation>.Value 


For Each item In query 
    Response.Write("<h3>" & item.eventtitle & "</h3>") 
    Response.Write(item.eventlocation & "<br />") 
Next 

回答

1

你需要調用Any,像這樣:

Dim query = _ 
From c In calXML...<event> _ 
Where c.<eventtypes>.<eventtype>.Any(Function(t) [email protected] = "23") _ 
Select c.<eventtitle>.Value, c.<eventlocation>.Value 
+0

的功能是什麼意義? – mmcglynn 2010-03-08 20:38:36

+0

這是一個Lambda表達式。 http://msdn.microsoft.com/en-us/library/bb531253.aspx – SLaks 2010-03-08 20:39:45