从 LINQ to XML 查询获取单个字符串

发布于 2024-11-14 17:38:50 字数 812 浏览 2 评论 0原文

这是我想要做的:

string parseCode = from x in xml.Descendants("LogType")
                   where x.Attribute("ID").Value == string)ddlHistoryLogDefinitions.SelectedValue
                   select x.Attribute("ParseCode").Value;

但这会产生错误:“无法将类型 'System.Collections.Generic.IEnumerable' 隐式转换为 'string'”

x 只有一个值.Attribute("ParseCode") 但它坚持返回类型 IEnumerable。如何将该值提取到字符串中?

编辑:谢谢您的回复。这是对我有用的:

string parseCode = (from x in xml.Descendants("LogType")
                    where x.Attribute("ID").Value == (string) ddlHistoryLogDefinitions.SelectedValue
                    select (string) x.Attribute("ParseCode").Value).FirstOrDefault();

这个技巧将整个 linq 查询包装在 .FirstOrDefault() 之前的 () 中。

Here is what I want to do:

string parseCode = from x in xml.Descendants("LogType")
                   where x.Attribute("ID").Value == string)ddlHistoryLogDefinitions.SelectedValue
                   select x.Attribute("ParseCode").Value;

But that gives error: "Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'string'"

There will only be one value for x.Attribute("ParseCode") but it insists on returning type IEnumerable<string>. How can I extract that value into a string ?

EDIT: Thank you for your responses. Here is what worked for me:

string parseCode = (from x in xml.Descendants("LogType")
                    where x.Attribute("ID").Value == (string) ddlHistoryLogDefinitions.SelectedValue
                    select (string) x.Attribute("ParseCode").Value).FirstOrDefault();

This trick was wrapping the entire linq query in () before the .FirstOrDefault().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

秋意浓 2024-11-21 17:38:50

如果您知道只有一个结果,请使用 .Single 选择唯一的结果:

string parseCode = (from x in xml.Descendants("LogType")
                   where x.Attribute("ID").Value == string)ddlHistoryLogDefinitions.SelectedValue
                   select x.Attribute("ParseCode").Value).Single();

使用 .SingleOrDefault.First 如果可能存在分别没有或超过一个。

Use .Single to select the only result if you know there will be one and only one:

string parseCode = (from x in xml.Descendants("LogType")
                   where x.Attribute("ID").Value == string)ddlHistoryLogDefinitions.SelectedValue
                   select x.Attribute("ParseCode").Value).Single();

Use .SingleOrDefault or .First If there might be none or more then one respectively.

扶醉桌前 2024-11-21 17:38:50

您查询返回一个集合。如果您需要第一个找到的 LogType 节点,那么您可以执行以下操作:

string parseCode = xml.Descendants("LogType")
    .Where(x => x.Attribute("ID").Value == (string)ddlHistoryLogDefinitions.SelectedValue)
    .Select(arg => x.Attribute("ParseCode").Value)
    .FirstOrDefault();

如果未找到元素,则返回 null

You query returns a collection. If you need the first found LogType node, then you can do something like this:

string parseCode = xml.Descendants("LogType")
    .Where(x => x.Attribute("ID").Value == (string)ddlHistoryLogDefinitions.SelectedValue)
    .Select(arg => x.Attribute("ParseCode").Value)
    .FirstOrDefault();

Will return null if no element found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文