2013-08-20 103 views
0

我正在尋找一些建立LINQ查詢的幫助。我想要獲得特定數據類型的所有選項卡。我開始建立查詢,但似乎仍然存在一些問題。欣賞任何feedback.Thanks周杰倫基於父屬性的過濾器

<DataType name="WELL_INDUSTRY"> 
    <Database key1="key1" key2="" delimeter=""> 
     <Tabs> 
     <Tab> 
      <Name>Basic</Name> 
var tabs = from tab in doc.Descendants("Tab") 
      where tab.Parent.Parent.Attribute("Name").ToString() == "WELL_INDUSTRY" 
      select new 
      { 
       Name = tab.Descendants("Name").First().Value 
      }; 

foreach (var tab in tabs) 
    Debug.WriteLine(tab.Name); 

回答

0

我會說,你從問題的反面開始。爲什麼不先找到<DataType>,然後選擇全部包含<Tab> s?

var tabs = from dt in doc.Descendants("DataType") 
      where (string)dt.Attribute("Name") == "WELL_INDUSTRY" 
      from tab in dt.Elements("Database").Elements("Tabs").Elements("Tab") 
      select new 
      { 
       Name = (string)tab.Elements("Name").First() 
      }; 

我也建議使用Element/Elements適當元素名稱更嚴格比賽中換人doc.Descendants("DataType")。它會使查詢更快。我不能爲你做,因爲我不知道整個文檔結構。

+0

它就像一個魅力。謝謝 – jay