C# How to use LINQ to grab all cell data under a particular column header name

Viewed 271

Given a list of column header names, I want to parse an HTML table using HTMLAgilityPack, to retrieve all table data under that column.

I can't seem to figure out how to apply LINQ towards this.

I am grabbing the table header column names like so...

//Select HTML Table
var table = htmlDoc.DocumentNode.SelectSingleNode("//table");
List<string> headerColumnNames = new List<string>();

foreach (HtmlNode tableHeader in table.SelectNodes("tr[1]/th"))
{
   headerColumnNames.Add(tableHeader.InnerText.Trim());              
}

(If you know how to do this in LINQ please feel free to give me an example).

Now I want to do something like

foreach (string name in headerColumnNames){

//SELECT all table data 
//WHERE the table column.name == name
//return a List of strings representing each cell value

}

I can't seem to figure out how to accomplish this using LINQ.

1 Answers
var headerColumnNames = htmlDoc.DocumentNode.SelectSingleNode("//table[1]")
    .SelectNodes("./thead[1]/tr[1]/th/text()")
    .Select(th => th.InnerText.Trim());
Related