4
我有一個查詢thatworks罰款,如果它返回一個row.However,如果有多個行,它會:.NET的LINQ to XML:終值覆蓋以前的不同值
- 創建正確的數量行
- 錯誤地將每行分配給最後一行的值......在這種情況下,它將返回x3行,因爲這是滿足查詢的最終節點。
我曾預料它會返回x3 DISTINCT行。 任何人都可以提出爲什麼不同的值被最終的XML節點覆蓋?
感謝
<?xml version="1.0" encoding="utf-8" ?>
<Music>
<customer id="89">
<name value="Sample Name 2" />
<age value="31" />
<location>
<city value="Wichita" />
<state value="KS" />
<country value="USA" />
</location>
</customer>
<customer id="80">
<name value="Sample Name 3" />
<age value="41" />
<location>
<city value="Seattle" />
<state value="WA" />
<country value="USA" />
</location>
</customer>
<customer id="84">
<name value="Sample Name" />
<age value="29" />
<location>
<city value="Los Angeles" />
<state value="CA" />
<country value="USA" />
</location>
</customer>
<customer id="100">
<name value="Rock UK" />
<age value="25" />
<location>
<city value="Los Angeles" />
<state value="CA" />
<country value="USA" />
</location>
</customer>
</Music>
的代碼如下:
private List<Music> currentMusic { get; set; }
private IEnumerable<Music> GetCustomer(string id)
{
XElement main = XElement.Load(@"D:\work\web\App_Data\Customers.xml");
IEnumerable<XElement> searched =
from c in main.Elements("customer")
where (string)c.Attribute("id") != "100"
select c;
Music music = new Music();
currentMusic = new List<Music>();
foreach (XElement customer in searched)
{
music.Country = customer.Element("name").Attribute("value").Value;
music.Name = customer.Element("age").Attribute("value").Value;
currentMusic.Add(music);
}
return currentMusic;
}
親愛的哦,親愛的,我看不到樹木的木頭。感謝芽 –