好吧,有沒有簡單的方法來返回FIRST LinqToXML結果?
我有一個LinqToXml查詢,返回單個記錄(正確的術語?),
不過,我不希望使用的..循環來拉,一個記錄從查詢。有更容易的方法嗎?
*獎金,如果你找到一個更好的方式來編寫查詢(我還在學習)
我的XML文件:
<?xml version="1.0"?>
<!-- first draft of LibMappings.xml file -->
<LibMappings>
<File>
<Name>Foundation.dll</Name>
<Map>Lib\Foundation</Map>
</File>
</LibMappings>
我的代碼:
private static string GetMapFor(string dllName)
{
//Opens Xml File and gets "Lib" map for dllName
string retVal;
XDocument libMapFile = XDocument.Load(_libMapFilePath);
//This query will return 1 record
IEnumerable<XElement> query = from c in libMapFile.Descendants("File")
where (string)c.Element("Name") == dllName
select c.Element("Map");
if (query.Count() == 1)
{
//there's only gonna be one here.. I might as well do this..
foreach (string x in query)
{
retVal = x;
break;
}
//I'd like to do this:
retVal = query[0];
}
return retVal;
}