如果它返回单行,我有一个可以正常工作的查询。但是,如果有多行,它将:
- 创建正确数量的行
- 错误地将每一行分配给最后一行的值...在这种情况下,它将返回 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;
}