AsEnumerable & WCF RRS feed

  • Question

  • Hi,


    My DAL and BLL communicate throw WCF. I have an issue when I return my order list by calling AsEnumerable on my query ("The underlying connection was closed") . Changing AsEnumerable  to ToList is working well.


    AsEnumerable is not supposed to run the query and convert IQueryable to IEnumerable ?


    Thanks In advance for your answers.

    Friday, June 27, 2008 11:12 AM


  • AsEnumerable does nothing to a query. It simply casts the query down to IEnumerable<X> and is useful in cases where you don't have the ability to state 'X' in the source. (like with anonymous types.) 


    The problem you are having is that you are returning a query to a service that will enumerate it (causing it to execute) and you are likely disposing of  the DataContext before that happens.  So your solution is to either use ToList() to execute the query before you return the results for serialization or to not dispose the DataContext.  It is completely save not to directly dispose the DataContext.  It won't leak memory, the connection will get closed, as long as you actually execute the query and enumerate all the results.




    Friday, June 27, 2008 3:33 PM