Developer, Former MVP, now at Microsoft - Best of 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
var result = from x in source where [some condition] select [something]
var result = from x in source.AsParallel() where [some condition] select [something]
var result = source.Where(x => [some condition]).Select(x => [something]);
var result = source.AsParallel().Where(x => [some condition]).Select(x => [something]);
static void Main() { Stopwatch sw = Stopwatch.StartNew(); DoIt(); Console.WriteLine("Elapsed = " + sw.ElapsedMilliseconds.ToString()); Console.ReadLine(); } static bool IsPrime(int p) { int upperBound = (int)Math.Sqrt(p); for (int i = 2; i <= upperBound; i++) { if (p % i == 0) return false; } return true; }
static void DoIt() { IEnumerable arr = Enumerable.Range(2, 4000000); var q = from n in arr where IsPrime(n) select n.ToString(); List list = q.ToList(); Console.WriteLine(list.Count.ToString()); }
from n in arr.AsParallel()