开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜