如何修复我的 linq 查询
这是示例 XML
<?xml version="1.0" encoding="utf-8" ?>
<Instructions>
<Instruction>
<Brand>Brand1</Brand>
<Text>
this is text for Brand1
</Text>
</Instruction>
<Instruction>
<Brand>Brand2</Brand>
<Text>
Brand2 text is slightly different
</Text>
</Instruction>
<Instruction>
<Brand>Brand3</Brand>
<Text>
Brand3 has boring text
</Text>
</Instruction>
<Instruction>
<Brand>Brand4</Brand>
<Text>
Brand4 had long text until the editor got hold of this file
</Text>
</Instruction>
</Instructions>
我的代码是这样的:
string WhoAmI = "Brand1";
string t =
(from Instruction in xmlDoc.Descendants("Instruction")
where (string)Instruction.Element("Brand").Value == WhoAmI
select t = Instruction.Element("Text").Value
).ToString();
//end code
t 总是
System.Linq.Enumerable+WhereSelectEnumerableIterator`2 [System.Xml.Linq.XElement,System.String]
不
这是 Brand1 的文字
我做错了什么?
Here's the sample XML
<?xml version="1.0" encoding="utf-8" ?>
<Instructions>
<Instruction>
<Brand>Brand1</Brand>
<Text>
this is text for Brand1
</Text>
</Instruction>
<Instruction>
<Brand>Brand2</Brand>
<Text>
Brand2 text is slightly different
</Text>
</Instruction>
<Instruction>
<Brand>Brand3</Brand>
<Text>
Brand3 has boring text
</Text>
</Instruction>
<Instruction>
<Brand>Brand4</Brand>
<Text>
Brand4 had long text until the editor got hold of this file
</Text>
</Instruction>
</Instructions>
My code is this:
string WhoAmI = "Brand1";
string t =
(from Instruction in xmlDoc.Descendants("Instruction")
where (string)Instruction.Element("Brand").Value == WhoAmI
select t = Instruction.Element("Text").Value
).ToString();
//end code
t is always
System.Linq.Enumerable+WhereSelectEnumerableIterator`2
[System.Xml.Linq.XElement,System.String]
not
this is text for Brand1
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
LINQ 语句返回值序列,而不是单个值。因此,在序列对象上调用
.ToString()
通常不会给您带来任何特别有用的东西。在这种情况下,您的语句返回一个序列,其中包含一个值,但仍然是一个序列。因此,您需要编写一条仅返回一个值的 LINQ 语句:
这里还需要考虑其他含义,例如如果序列为空,
First()
将引发异常。FirstOrDefault()
将返回 null。A LINQ statement returns a sequence of values, not a single value. So calling
.ToString()
on a sequence object will, ordinarily, not give you anything particularly useful.In this case, your statement is returning a sequence with one value in it, but still a sequence. So you need to write a LINQ statement that only returns one value:
There's other implications to consider here, such that
First()
will throw an exception if the sequence is empty.FirstOrDefault()
will return null instead.此查询将返回字符串序列而不是单个字符串。因此 .ToString() 方法调用将是 IEnumerable ToString 方法。
如果您确信查询始终只返回 1 个字符串,则可以使用 Single() 或 SingleOrDefault() 方法仅返回该字符串。
This query will return a sequence of strings not a single string. So the .ToString() method call will be the IEnumerable ToString method.
If you are confident that your query will always only return 1 string you can use the Single() or SingleOrDefault() methods to just return the string.
尝试将
ToString()
替换为FirstOrDefault()
Try replacing
ToString()
withFirstOrDefault()
这将为您提供文本节点包含的文本值的集合。但是,如果缺少文本节点或品牌节点,则此查询将失败。您发布的代码所做的事情是不接受集合作为结果;您将返回的集合对象转换为字符串,默认情况下该字符串仅提供对象的名称。
您将需要循环遍历查询返回的列表,以对返回的多个值执行一些有用的操作...
This will give you a collection of the Text nodes' contained text values. However, if either a Text node or a Brand node is missing this query will fail. What the code you posted was doing was not accepting a collection as a result; you were casting the collection object returned to a string which by default gives you just the name of the object.
You will need to loop through the list returned by the query to do something useful with the multiple values returned...