I have some Linq that searches for a key word in a text file, skips three rows when it finds it and then returns all rows below until it reaches a row that equals a double space.
var line = File.ReadLines(path)
            .SkipWhile(line => !line.Contains(searchText))
            .Skip(3)
            .TakeWhile(line => !line.Equals("  "));
I’d like to add some error handling in there, for example if the key word can’t be found or there aren’t three rows to skip through.
I was trying to apply this answer but I can’t get the Syntax right. Is it possible to handle exceptions within LINQ queries?
This is what I know have, but it still seems to throw an error System.IO.IOException' in System.Private.CoreLib.dll when the string can not be found in the text file.
What am I doing wrong here?
var line = File.ReadLines(path)
            .SkipWhile(line => !line.Contains(searchText))
            .Skip(3)
            .TakeWhile(line => 
            {
                try
                {
                    return !line.Equals("  ");
                }
                catch (Exception)
                {
                    return false;
                    //
                }
            });
