For some test code I needed to write a small utility that kills all processes running that matches a given name. Like KillApp Devenv.
After writing the code as I normally would, I thought I should Linquify it. So here what I cooked up
static class Program { static void Main(string[] args) { if (args.Length < 1 || args[0] == "-?") { Console.WriteLine("Usage: KillApp "); return; } (from p in Process.GetProcesses() where p.ProcessName.Contains(args[0]) select p).ForEach(p => p.Kill()); } public static void ForEach<T>(this IEnumerable<T> list, Action<T> act) { foreach (T t in list) act(t); } }
Sheer beauty. The only glitch was that I had to write a ForEach extension method :( .
Since I never liked DB's a lot (that doesn't include our test manager Dinesh Bhat though) I converted the Linq query to extension method style as in
Process.GetProcesses().Where(p=>p.ProcessName.Contains(args[0])).ForEach(p=>p.Kill());
Thats when I started wondering is this Lisp I'm coding in or C#? Then the order of the parenthesis reminded me it is indeed C#. With Linq life is good :) the only thing that can make me