As Eric Lippert described in this article, yield return
is not allowed within try/catch
clauses.
Is there a nice way I could get something like this, without having to write my own IEnumerator
by hand:
public IEnumerable<Data> GetData()
{
var transaction = Session.BeginTransaction());
try
{
IQuery q = CreateQuery(session);
foreach (var result in q.Enumerable())
yield return ProjectResult(result); // <-- doesn't work
session.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw;
}
finally
{
transaction.Dispose();
}
}
I'd just change the transaction-handling logic like this:
Or if your transaction supports the idea of "dispose means rollback unless it's commited":
Re-factor
into a separate method and simply return it's result.