我需要能够从select块返回数据。我该怎么做?
public double loadData()
{
XDocument loadedData = XDocument.Load("XMLFILE1.xml");
var data = from query in loadedData.Descendants("ScoreData")
select new Score
{
HighScore = (Double)query.Element("HS"),
};
return highScore;
}
答案 0 :(得分:1)
如果您需要单个值,那么为什么要创建Score
实例?
这将返回First
分数值:
return (from query in loadedData.Descendants("ScoreData")
select (Double)query.Element("HS")).First();
这将返回Maximum
分数值:
return (from query in loadedData.Descendants("ScoreData")
select (Double)query.Element("HS")).Max();
如果您需要获得所有分数,则应将返回类型更改为double[]
public double[] loadData()
{
XDocument loadedData = XDocument.Load("XMLFILE1.xml");
return (from query in loadedData.Descendants("ScoreData")
select (Double)query.Element("HS")).ToArray();
}