Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6372949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:18:11+00:00 2026-05-25T01:18:11+00:00

As Eric Lippert described in this article , yield return is not allowed within

  • 0

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:18:12+00:00Added an answer on May 25, 2026 at 1:18 am

    I’d just change the transaction-handling logic like this:

    public IEnumerable<Data> GetData()
    {
        var transaction = Session.BeginTransaction();
        bool rollback = true;
        try 
        {
            IQuery q = CreateQuery(session);
    
            foreach (var result in q.Enumerable())
            {
                yield return ProjectResult(result);
            }
    
            rollback = false;
            session.Commit();
        }
        finally
        {
            if (rollback)
            {
                transaction.Rollback();
            }
            transaction.Dispose();
        }
    }
    

    Or if your transaction supports the idea of “dispose means rollback unless it’s commited”:

    public IEnumerable<Data> GetData()
    {
        using (var transaction = Session.BeginTransaction();
        {
            IQuery q = CreateQuery(session);
    
            foreach (var result in q.Enumerable())
            {
                yield return ProjectResult(result);
            }
    
            // Commits the tnrasaction, so disposing it won't roll it back.
            session.Commit();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is partially inspired by this article written by Eric Lippert: http://blogs.msdn.com/ericlippert/archive/2009/10/19/what-is-this-thing-you-call-thread-safe.aspx Using
Eric Lippert's comments in this question have left me thoroughly confused. What is the
I know, I know... Eric Lippert's answer to this kind of question is usually
If you forward to approximately 13 minutes into this video by Eric Lippert he
Eric Lippert told me I should try to always make value types immutable ,
Here's Eric Lippert's comment from this post : Now that you know the answer,
This question and Eric Lippert's answer got me wondering: How do you decide whether
Related to this question , based on a comment of user Eric Lippert .
The following is okay: try { Console.WriteLine("Before"); yield return 1; Console.WriteLine("After"); } finally {
In another topic , a nice guy told me by quote Eric Lippert's words:The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.