1

I have the following function that iterates some rows and searches for files in a database. A null gets returned by the repository if file is not found in the database and the function should then return an empty ResultRow. The Assert() after the if statement fires in some cases. Why? How is that possible?

IEnumerable<ResultRow> DoRows(SequenceListWithQc list, 
IList<TestSpecification> testSpecs, bool writeResults=false)
{
    foreach (var row in list.Rows)
    {
        var result = new ResultRow();

        result.FileName = row.Columns[list.Headers.IndexOf("File Name")];

        var rawFile = repository.GetRawFileByFilename(result.FileName);

        if (rawFile == null)
        {
            yield return result;
        }

        Debug.Assert(rawFile != null);
    }
}
5
  • Immediately after the yield? It is also possible that we go for another loop and that in the next loop, rawFile is null... Commented May 24, 2017 at 13:01
  • 5
    yield return does not return from the function Commented May 24, 2017 at 13:01
  • 3
    Did you try stepping through your code via a debugger? Commented May 24, 2017 at 13:01
  • :) Ok silly me. yield return obviously doesn't return. Commented May 24, 2017 at 13:09
  • Another issue I see is you are doing row.Columns[list.Headers.IndexOf("File Name")], if the file is not found, you have an index of -1 and thus an OutOfRangeException. Commented May 24, 2017 at 13:09

1 Answer 1

2

You can call continue to move onto the next row in the loop

    if (rawFile == null)
    {
        yield return result;
        continue;
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.