none
The result of a query cannot be enumerated more than once. RRS feed

  • Question

  • I use procedure in database and I have:

    IQueryable<GetCategoryCatagoriesProcedureResult> categories = db.GetCategoryCatagoriesProcedure(5).AsQueryable();
    
    var count = categories.ToList().Count();
    
    if(count > 1)
    
    {
    
    foreach (var item in categories) { ... }
    
    }


    and I have error: The result of a query cannot be enumerated more than once. - why?

     

    Friday, November 11, 2011 4:30 PM

Answers

  • Hi green_green;

    Try changing the code to this:

    var categories = db.GetCategoryCatagoriesProcedure(5).AsQueryable().ToList();

    var count = categories.Count();

    if(count > 1)
    {
        foreach (var item in categories) { ... }
    }

    But this should work as well:

    var categories = db.GetCategoryCatagoriesProcedure(5).AsQueryable().ToList();

    foreach (var item in categories) { ... }

    If the List is empty it will never go into the fereach loop and you do not have to test to see if it has anything.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by green_green Friday, November 11, 2011 6:37 PM
    Friday, November 11, 2011 6:12 PM