samedi 26 août 2017

Add string to array if condition found using coalesce operator

I know I can test the condition and run the code like this:

if (scrapedElement.Contains(".html")
     string [] Test = new string[] { scrapedElement, string.empty }
else 
     string [] Test = new string[] { scrapedElement }

However, I want to do it in a single line if possible. Something similar to this (this is the full line of code that I want it to work in):

File.AppendAllLines(@"C:\Users\DJB\Documents\Visual Studio 2017\Projects\TempFiles\WebScraperExport.csv", new[] { (scrapedElement.Contains(".html") ? scrapedElement, string.Empty : scrapedElement)});

What I am doing is a web scraper that then saves the files in an excel file. For every element it finds that is a link, add a blank line after it, if not just add the element.

Aucun commentaire:

Enregistrer un commentaire