C #: Combining AddRange () and Lambda Expressions

I have a Collectionnode, in particular a set of nodes HTMLAgilityPack.HTMLNodeCollection. I know that I can retrieve and store information from nodes by doing:

List<string> nodesList = new List<string>();

var nodes = myFile.DocumentNode.SelectNodes(file_path);
foreach (var node in nodes)
    nodesList.Add(node.InnerText);

However, what I would like to do is apply AddRange()and not add elements in the loop foreach. I am trying to use a lambda expression for this, but I am not familiar enough with lambda expressions to do it right. I'm trying to combine something like

nodesList.AddRange( string[] arr = x => nodes.ToArray()[x].InnerText );

I know it doesn't make any syntactical sense, but I'm not sure if this can be done. I would like to just Cast<string>()collect, but I need to extract the text from each node.

Any ideas on how to put this in an expression AddRange()?

+4
source share
1 answer

You are looking for :IEnumerable.Select()

nodesList.AddRange(nodes.Select(node => node.InnerText))
+4
source

Source: https://habr.com/ru/post/1568252/


All Articles