如何修复我的 linq 查询

发布于 2024-09-10 20:32:17 字数 1318 浏览 3 评论 0原文

这是示例 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还在原地等你 2024-09-17 20:32:17

LINQ 语句返回值序列,而不是单个值。因此,在序列对象上调用 .ToString() 通常不会给您带来任何特别有用的东西。

在这种情况下,您的语句返回一个序列,其中包含一个值,但仍然是一个序列。因此,您需要编写一条仅返回一个值的 LINQ 语句:

string t = (from ... select ...).First();

这里还需要考虑其他含义,例如如果序列为空,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:

string t = (from ... select ...).First();

There's other implications to consider here, such that First() will throw an exception if the sequence is empty. FirstOrDefault() will return null instead.

情场扛把子 2024-09-17 20:32:17

此查询将返回字符串序列而不是单个字符串。因此 .ToString() 方法调用将是 IEnumerable ToString 方法。

如果您确信查询始终只返回 1 个字符串,则可以使用 Single() 或 SingleOrDefault() 方法仅返回该字符串。

string WhoAmI = "Brand1"; 
string t = 
              (from Instruction in xmlDoc.Descendants("Instruction") 
               where (string)Instruction.Element("Brand").Value == WhoAmI 
               select t = Instruction.Element("Text").Value 
               ).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.

string WhoAmI = "Brand1"; 
string t = 
              (from Instruction in xmlDoc.Descendants("Instruction") 
               where (string)Instruction.Element("Brand").Value == WhoAmI 
               select t = Instruction.Element("Text").Value 
               ).SingleOrDefault();
揪着可爱 2024-09-17 20:32:17

尝试将 ToString() 替换为 FirstOrDefault()

Try replacing ToString() with FirstOrDefault()

薯片软お妹 2024-09-17 20:32:17

这将为您提供文本节点包含的文本值的集合。但是,如果缺少文本节点或品牌节点,则此查询将失败。您发布的代码所做的事情是不接受集合作为结果;您将返回的集合对象转换为字符串,默认情况下该字符串仅提供对象的名称。

您将需要循环遍历查询返回的列表,以对返回的多个值执行一些有用的操作...

        var results = (from e in doc.Descendants("Instruction")
                       where e.Descendants("Brand").First().Value == WhoAmI
                       select e.Descendants("Text").First().Value).ToList();

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...

        var results = (from e in doc.Descendants("Instruction")
                       where e.Descendants("Brand").First().Value == WhoAmI
                       select e.Descendants("Text").First().Value).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文