Can't Debug My Linq Query?
In my code below, when it gets to the foreach loop, I run my debugger until it highlights "result" and then it just stops running the code at that point, and there are no exceptions thrown or anything.
I really have no idea why it would not give me any error messag开发者_如何学编程es when there is something going wrong here.
var result =
from a in db.table
select new {table = a};
foreach(var row in result){
...
}
When debugging Linq queries, it often makes life easier if, instead of trying to examine the contents of your IQueryable
in the debugger, you flatten it out to a list or array.
Try putting:
var resultList = result.ToList();
..after your query; put a breakpoint directly after that line; and then see in the debugger what the contents of resultList
are.
I would guess it's actually traversing over the list. If you have a large table or a complicated query it will take a while (seconds) to execute.
Does it ever go beyond the foreach loop?
Perhaps you are waiting for rows to come from a database, and there is lock contention causing your app to stall and wait for the lock(s) on the table you are accessing to be released. That is not an error condition and no exception should be thrown. For example, are there writers that are concurrently writing to the table you are trying to access or other accessors that have acquired an exclusive table lock, especially in a long transaction?
精彩评论