2012-01-10 38 views
0

我有一些醜陋的XML(如果我可以改變我會)來找我,我需要能夠處理。我想通過XML中的特定屬性進行分組以供進一步處理。問題是某些屬性的type =「A」,有些屬於type =「A1」,type =「A2」等。我想要所有具有以「A」開頭的屬性類型值的元素,被分組在一起。我怎麼能從LINQ子串字符串屬性值

這不是工作,我不知道是什麼改變來得到它的工作:

var result = from ele in xdoc.Descendants(Namespace + "item") 
       let item= ele.Attribute("type").Value.Substring(1,1) 
       group ele by item into grp 
       select grp; 

當我脫下它工作正常的子字符串,但顯然不組我多麼希望。

錯誤:

Error has been thrown by the target of an invocation 

示例XML:

<items> 
<item type="A" position="0"> 
    <itemvalue>10</itemvalue> 
</item> 
    <item type="A1" position="1"> 
    <itemvalue>20</itemvalue> 
</item> 
    <item type="A2" position="2"> 
    <itemvalue>30</itemvalue> 
</item> 
    <item type="B" position="0"> 
    <itemvalue>10</itemvalue> 
    </item> 
    <item type="B1" position="1"> 
    <itemvalue>20</itemvalue> 
    </item> 
    <item type="B2" position="2"> 
    <itemvalue>30</itemvalue> 
</item> 
</items> 

回答

2

沒有在我手上一個編譯器和打字我的手機,我會說,你應該作爲第一個參數傳遞0至Substring,即Substring(0,1)

+0

哦,我的天哪,感謝讓我看起來像一個完全白癡......這完全是謝謝@afrischke。我是一名sql開發人員,甚至沒有考慮子字符串中的0位置。 – scarpacci 2012-01-10 17:22:11

+0

不要擔心,我已經(當然會造成)自己更尷尬的錯誤。 – afrischke 2012-01-10 23:34:17

相關問題