× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

How to debug a Linq Lambda Expression?

Leniel Macaferi
Feb 02, 2015
<p>Make sure you read the official MSDN doc on this matter:</p> <p><a href="http://msdn.microsoft.com/en-us/library/bb385795.aspx#bkmk_steppingandlinq">Debugging LINQ</a></p> <p>and please vote for this suggestion on Visual Studio's User Voice page:</p> <p><a href="http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2049821-debug-lambda-expressions">Debug Lambda expressions</a></p> <p><a href="http://visualstudio.uservoice.com/users/25166487-allon-guralnek">Allon Guralnek</a> comments on <em>March 18, 2014 12:37 PM</em> about a way of setting a breakpoint with the keyboard only:</p> <blockquote> <p>@Anonymous: You can do this today by setting a breakpoint inside the lambda, thereby enabling you to inspect each value that comes in and out of the lambda. As far as I know, you can't set a breakpoint inside a lambda using the mouse, you must use the keyboard. Put the cursor inside the lambda body (e.g. on the first token, or anything after the =&gt; and the whitespace that follows) then press F9 (or whatever keyboard shortcut you use to place a breakpoint). Only the inside of the lambda statement will turn red, and the debugger will break there for each item the lambda is evaluated against (so for an array of 100 items, using .Where() will cause the breakpoint to hit 100 times).</p> </blockquote> <p>Here's it in action in my current Visual Studio 2013:</p> <p><img src="http://i.stack.imgur.com/f3U1M.png" alt="enter image description here"></p> <p>As you can see it works pretty well and allows us to see the value of a given property being tested. This is for sure an awesome tool/life saver! :)</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/17231101/How%20to%20debug%20a%20Linq%20Lambda%20Expression?/22518808">Stack Overflow</a>.</p>
comments powered by Disqus