Archive

Archive for March, 2017

Linq vs Foreach

March 28, 2017 Leave a comment

What could be best approach Linq vs Foreach ?

string[] words = { “summer”, “winter”, “spring”, “autumn” };
//Iterate through the words[]
foreach(string s in words)
{
if (s.Contains(“autumn”))
Console.WriteLine(s);
}

// Linq on words
var shortWords =
from word in words
where word.Contains(“autumn”)
select word;

// Print word out
shortWords.Dump();

 

Advertisements
Categories: Uncategorized