How did we cope before Linq?

I needed to parse log files and find all the instance of some portfolio names from between the words "filter" and "and". 30 secs of code later.

namespace Grep
{
    class Program {
        static void Main(string[] args)
        {
            var lines = File.ReadAllLines(@"filename.txt");
            var regex = new Regex(@"filter\s(?<filterName>\w+)\sand");
            var filtered = from line in lines
                           let match = regex.Match(line)
                           where match.Success
                           select match.Groups["filterName"].Value;
            foreach (var x in filtered.Distinct())
                Console.WriteLine(x);
            Console.ReadLine();
        }
    }
}

Its just so easy ;-)

Comments are closed