SuperDave
SuperDave

Reputation: 413

Simplifying a Linq search in an HTMLAgilitypack document

I have this code which works but seems like I have missed something as it should all be done in the first call.

Trying to integrate any reference to the ElementAt[2] in my tries just returns nothing.

        var data = doc.DocumentNode.Descendants(0)
            .Where(n => n.HasClass("last"))
            .Select(tr => new {tr, tds = tr.Descendants().ToList()})
            .Select(t => new {Last = t.tds[0].InnerText.Trim()});

        textBox1.Text = data.ElementAt(2).Last;

There should be a syntax that wraps this all in the original Linq query that returns the string in the data variable, but I'm stuck or missing something.

Upvotes: 2

Views: 41

Answers (1)

Hugo
Hugo

Reputation: 479

Have you tried:

var data = doc.DocumentNode.Descendants(0)
    .Where(n => n.HasClass("last"))
    .Select(tr => new {tr, tds = tr.Descendants().ToList()})
    .Select(t => new {Last = t.tds[0].InnerText.Trim()})
    .ElementAt(2)
    .Last;

Upvotes: 1

Related Questions