Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug a Linq Lambda Expression?

I am using Entity Framework and Linq to Entitites.

I would like to know if there is any way in Visual Studio 2012 to debug this code, step by step. At the moment when placing a break point, the cursor goes over it but does not step inside.

I am more interested to see the value of x.e... not the sql generated for example.

Notes: I'm fine with using other tools or Visual Studio plugins.

          IEnumerable<EventPushNotification> eventToPushCollage = eventsForEvaluation
                    .GroupJoin(eventCustomRepository.FindAllPushedEvents(),
                        e => e.Id,
                        p => p.PushedEventId,
                        (e, p) => new { e, p })
                     .Where(x => x.e.DateTimeStart > currentDateTime &&
                        currentDateTime >= x.e.DateTimeStart.AddMinutes(defaultReminders) &&     //  Data from default reminder for collage event in web.config  
                        x.p.Count() == 0)                                           // Check if the Event has not being already pushed
                     .Select(y => new EventPushNotification
                     {
                         Id = y.e.Id,
                         EventTitle = y.e.EventTitle,
                         DateTimeStart = y.e.DateTimeStart,
                         DateTimeEnd = y.e.DateTimeEnd,
                         Location = y.e.Location,
                         Description = y.e.Description,
                         DeviceToken = y.e.DeviceToken
                     });
like image 376
GibboK Avatar asked Jun 21 '13 08:06

GibboK


People also ask

How to debug a lambda expression c#?

Put the cursor inside the lambda body (e.g. on the first token, or anything after the => and the whitespace that follows) then press F9 (or whatever keyboard shortcut you use to place a breakpoint).

Can we debug lambda expression?

We can use different IDE's like Netbeans, IntelliJ, and Eclipse to debug the lambda expressions in Java. It is always possible to create multi-line lambda expressions and use print statements to display the values of a variable. The debugger can also provide additional information about the state of a java program.

What does => mean in Linq?

The => operator can be used in two ways in C#: As the lambda operator in a lambda expression, it separates the input variables from the lambda body. In an expression body definition, it separates a member name from the member implementation.


3 Answers

Make sure you read the official MSDN doc on this matter:

Debugging LINQ

and please vote for this suggestion on Visual Studio's User Voice page:

Debug Lambda expressions

Allon Guralnek comments on March 18, 2014 12:37 PM about a way of setting a breakpoint with the keyboard only:

@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 => 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).

Here's it in action in my current Visual Studio 2013:

enter image description here

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! :)

like image 58
Leniel Maccaferri Avatar answered Oct 11 '22 13:10

Leniel Maccaferri


You can't debug a Lambda expression if you're using a Linq to Entities provider.

But you can take a look at what SQL it translate into. Also if you are willing to suffer a performance hit - you could load it all into Linq to obejcts - and do a Step by step

like image 33
Jens Kloster Avatar answered Oct 11 '22 13:10

Jens Kloster


You can add breakpoints on any of your own code.

So put the cursor at 'x.e', and press F9.

like image 44
Maarten Avatar answered Oct 11 '22 14:10

Maarten