tags:

views:

37

answers:

2

I'm making a database call with linq and it is returning some results to me. Following is the code for the same:

var resultSet = DataContext.GetList(id);

foreach(var result in resultSet)
{
    // do something here with result
}

After this, I try to access again same resultSet as below:

foreach(var result in resultSet)
{
    // do something here with result
}

When I'm in debug mode it doesn't throw any error, instead it simply exits debug mode and execution is completed and focus comes back to page.

I want to know why it is not throwing any error in debug mode that I'm using the enumeration for the second time? Am I missing anything? All other errors are throwing exceptions even in the debug mode.

Update: I intentionally didn't do that second calling. It was done by mistake, but it took sometime for me to find that error, if it would have thrown an error, then I would have easily fixed it. This is reason I posted this question here.

Note: I'm doing this throw a ajax call.

A: 

To make an informed decision we would really need to know what happens in between, however, it is possible that your context is perhaps being disposed before you call the query a second time?

If you plan to iterate over the result set more than once you may aswell cache the list on the first call to avoid making another unneccessary database call. LINQ only retrieves the data from the database once it is accessed i.e. each time you enumerate your result set.

Try doing this and see if it helps:

var resultSet = DataContext.GetList(id).ToList();

foreach(var result in resultSet)  
{  
    // do something here with result  
} 
...

foreach(var result in resultSet)  
{  
    // do something here with result  
} 
James
Thanks for the information. Actually I made that second call by mistake, I know that I can convert that to local list and use it as many times as I want. May be I'm not clear enough in my question. I wanted to know why it is not throwing any error. It took some time to realize that error is because I'm calling the enumeration for the second time. If it throws error then it would have saved my time in searching that mistake. I hope my question is clear to you now.
JPReddy
@jayaprakash: Ah apologies I thought you actually wanted to iterate over the collection twice. Without seeing the complete code, I would hazard a guess at being something inside the ajax method. Do you have it wrapped in a try/catch block or anything?
James
+1  A: 

I think if you go to Debug (Menu)> Exceptions and check the checkbox under Thrown for Common Language Runtime Errors. Now Visual Studio debugger should break when the error occurs and you should be able to see what's happening.

Ismail