I asked around while trying to learn how to use LINQ. LINQ is essentially SQL for .NET—it allows you to query objects directly in memory. For example:
List<int> test = new List<int> { 1, 3, 5 }; List<int> testnew = (from a in test where a == 5 select a).ToList(); Someone claimed LINQ was faster than using foreach for selecting items. So, I tested it.
The Benchmark static List<int> testlist = new List<int>(); static void Main(string[] args) { DateTime b, a; List<double> regularTimes = new List<double>(); List<double> LINQTimes = new List<double>(); for (int x = 0; x < 1000000; x++) testlist.Add(x * 5); // Foreach loop test for (int x = 0; x < 1000; x++) { b = DateTime.Now; TestRegular(); a = DateTime.Now; regularTimes.Add((a - b).TotalMilliseconds); } Console.WriteLine("Regular Avg.: {0}", regularTimes.Average()); // LINQ test for (int x = 0; x < 1000; x++) { b = DateTime.Now; TestLINQ(); a = DateTime.Now; LINQTimes.Add((a - b).TotalMilliseconds); } Console.WriteLine("LINQ Avg.: {0}", LINQTimes.Average()); Console.ReadLine(); } private static void TestRegular() { List<int> newlist = new List<int>(); foreach (int item in testlist) if (item % 2000 == 0) newlist.Add(item); } private static void TestLINQ() { var newlist = (from a in testlist where a % 2000 == 0 select a).ToList(); } This runs each selection method 1000 times on a list of 1,000,000 items.
...