开发者

Why can't I call FirstOrDefault on Descendants("XName")

开发者 https://www.devze.com 2023-03-25 11:50 出处:网络
Why can\'t I call FirstOrDefault() on this XML, whereas I have other XDocuments where FirstOrDefault() is available?

Why can't I call FirstOrDefault() on this XML, whereas I have other XDocuments where FirstOrDefault() is available?

        var actual = 开发者_运维知识库new XDocument(
            new XComment("This is a comment"),
            new XElement("Root",
                         new XElement("Child1", "data1"),
                         new XElement("Child2", "data2")
                ));

        actual.Descendants("Child1"). <- FirstOrDefault does not show up.


You should include System.Linq namespace in the top of your cs file.
If you're using ReSharper, try push ctrl+alt+space after typing . and look for FirstOrDefault. R# helps to manage namespaces.


It works for me:

    var actual = new XDocument(
        new XComment("This is a comment"),
        new XElement("Root",
                     new XElement("Child1", "data1"),
                     new XElement("Child2", "data2"),
            ));

    actual.Descendants("Child1").FirstOrDefault();

make sure you have using System.Linq in your using directives.

0

精彩评论

暂无评论...
验证码 换一张
取 消