A LINQ query

  • Thread starter Thread starter erxuan
  • Start date Start date
E

erxuan

I have a HtmlNode class that has a string property TagName and a int
property Level.
Now I have: List<HtmlNode> nodes;

Is it possible to write a Linq query that select as many nodes as
possible from this list and the selected nodes have the same TagName
and Level?

For example, if I have a list of 4 nodes with
node1.TagName = "span", node1.Level = 8;
node2.TagName = "span", node2.Level = 8;
node3.TagName = "table", node3.Level = 8;
node4.TagName = "span", node4.Level = 7;

The linq query is supposed to return node1 and node2.

Thanks!
 
I have a HtmlNode class that has a string property TagName and a int
property Level.
Now I have:  List<HtmlNode> nodes;

Is it possible to write a Linq query that select as many nodes as
possible from this list and the selected nodes have the same TagName
and Level?

For example, if I have a list of 4 nodes with
node1.TagName = "span", node1.Level = 8;
node2.TagName = "span", node2.Level = 8;
node3.TagName = "table", node3.Level = 8;
node4.TagName = "span", node4.Level = 7;

The linq query is supposed to return node1 and node2.

Assuming I've got you right, this should do the trick:

(from n in nodes
group n by new { n.TagName, n.Level } into ng
orderby ng.Count() descending
).First()
 
Back
Top