【发布时间】:2014-11-12 06:42:57
【问题描述】:
我对使用 LINQ 非常陌生。我有以下 XML:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<RetentionValue code="NR" fullName="Normal Retention">Used for normal retention periods commonly expressed as only numbers (i.e. "25" for 25 years).</RetentionValue>
<RetentionValue code="SR" fullName="Short Retention">Used for short retention periods expressed in terms of months (i.e. "6 months").</RetentionValue>
<RetentionValue code="AR" fullName="Annual Review">Review annually and retain what is needed, dispose of what is not needed.</RetentionValue>
<RetentionValue code="PE" fullName="Permanent">Document is to be retained permanently.</RetentionValue>
<RetentionValue code="EX" fullName="Expiration">Disposal date is calculated from expiration of a contract, loan, or other such instrument.</RetentionValue>
<RetentionValue code="LI" fullName="Lifetime">Disposal date is calculated from the end-of-life date for a piece of equipment or other asset (e.g. software application).</RetentionValue>
<RetentionValue code="TE" fullName="Employee Termination">Disposal date is calculated from the date of termination or retirement of an employee.</RetentionValue>
<RetentionValue code="FR" fullName="Final Resolution">Disposal date is calculated from the date of final resolution for an issue.</RetentionValue>
</root>
在我的代码中,我创建了一个具有属性RetentionEvent 的对象,该属性的值是上述两个字母代码之一。我想找到具有匹配属性的元素,然后将fullName 返回到一个文本字段,并将值(冗长的描述)返回到另一个文本字段。到目前为止,我有以下内容:
// Construct a RecordDisposal object by passing it a valid retention code.
// An exception will be thrown if the code is not valid.
_rd = new RecordDisposal(RetentionCode.Text);
// Load up XML file with description of codes.
XDocument rangeValues = XDocument.Load("DisposalRangeValues.xml");
XElement codeValue = rangeValues.Root.Elements().Single(x => (string)x.Attribute("code") == _rd.DisposalList[0].RetentionEvent);
CodeName.Text = codeValue.Attribute("fullName").Value.ToString();
CodeDescription.Text = codeValue.Value.ToString();
查询是this 代码的直接模式(我认为),但是我遇到了一些我不明白的错误:“查询正文必须以选择子句或组结尾子句”(我认为这就是 ToList() 的用途),并且 “select 子句中的表达式类型不正确。调用 'Select' 时类型推断失败。” em> 不幸的是,我对 Linq 的理解不够好,无法解决此问题,并且“有关此错误的帮助”没有任何用处。
我做错了什么?另外,我怀疑ToList() 函数会返回一些在ToString() 函数中渲染效果不佳的东西,但我一直在等着弄清楚其他东西,然后再愚弄它。如果有人有建议,请提供。
编辑:根据以下建议修改代码以使用 Single。发现rangeValues 正在将所有<root> 作为单个节点加载。
EDIT2:想通了。修改代码以改用rangeValues.Root.Elements()。更新了上面的代码以反映。
【问题讨论】: