C# 如何调试 Linq Lambda 表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17231101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to debug a Linq Lambda Expression?
提问by GibboK
I am using Entity Framework and Linq to Entitites.
我正在使用实体框架和 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.
我想知道在 Visual Studio 2012 中是否有任何方法可以逐步调试此代码。在放置断点的那一刻,光标会越过它但不会进入。
I am more interested to see the value of x.e... not the sql generated for example.
我更感兴趣的是看到 xe.. 的值,而不是例如生成的 sql。
Notes: I'm fine with using other tools or Visual Studio plugins.
注意:我可以使用其他工具或 Visual Studio 插件。
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
});
采纳答案by Jens Kloster
You can'tdebug a Lambda expression if you're using a Linq to Entities provider.
你不能,如果你正在使用LINQ到实体提供商调试Lambda表达式。
But you can take a lookat 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
但是你可以看看它转换成什么 SQL。此外,如果您愿意遭受性能损失 - 您可以将其全部加载到 Linq 中以进行对象 - 并逐步执行
回答by studert
I don't know how to this directly in Visual Studio, but you should have a look at LinqPad: http://www.linqpad.net/
我不知道如何直接在 Visual Studio 中执行此操作,但您应该看看 LinqPad:http: //www.linqpad.net/
回答by Maarten
You can add breakpoints on any of your own code.
您可以在您自己的任何代码上添加断点。
So put the cursor at 'x.e', and press F9.
所以把光标放在'x.e',然后按F9。
回答by Rafal
No there is no way to see values of xnor evariables because linq to orm is not executed it is translated/interpreted to generate an sql query.
不,没有办法查看x或e变量的值,因为没有执行 linq to orm 它被翻译/解释以生成 sql 查询。
回答by Leniel Maccaferri
Make sure you read the official MSDN doc on this matter:
请务必阅读有关此问题的官方 MSDN 文档:
and please vote for this suggestion on Visual Studio's User Voice page:
请在 Visual Studio 的用户语音页面上投票支持此建议:
Allon Guralnekcomments on March 18, 2014 12:37 PMabout a way of setting a breakpoint with the keyboard only:
Allon Guralnek于2014年3 月 18 日下午 12:37评论了一种仅使用键盘设置断点的方法:
@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).
@Anonymous:您今天可以通过在 lambda 内部设置断点来执行此操作,从而使您能够检查进出 lambda 的每个值。据我所知,您不能使用鼠标在 lambda 内设置断点,您必须使用键盘。将光标放在 lambda 主体内(例如,在第一个标记上,或在 => 和后面的空格之后的任何内容上),然后按 F9(或用于放置断点的任何键盘快捷键)。只有 lambda 语句的内部会变成红色,并且调试器会针对 lambda 所针对的每个项目在那里中断(因此对于 100 个项目的数组,使用 .Where() 将导致断点命中 100 次)。
Here's it in action in my current Visual Studio 2013:
这是在我当前的 Visual Studio 2013 中的操作:


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! :)
如您所见,它运行良好,并允许我们查看正在测试的给定属性的值。这肯定是一个很棒的工具/救生员!:)
回答by dotNET
For any future readers, this has now been included in Visual Studio. Starting from Visual Studio 2015 Preview, you can now debug lambda expression during debugging. All debug windows including Watch, QuickWatch and Immediate support lambda expression evaluation. You can read more about this here.
对于任何未来的读者,这现已包含在 Visual Studio 中。从 Visual Studio 2015 预览版开始,您现在可以在调试期间调试 lambda 表达式。所有调试窗口,包括 Watch、QuickWatch 和 Immediate 都支持 lambda 表达式评估。您可以在此处阅读更多相关信息。
回答by Miguel Rodriguez
I had to 'Enable Just My Code' in Tools/Options/Debugging. To see the different results between Lambda-methods, I've put .ToList() between them.
我必须在工具/选项/调试中“仅启用我的代码”。为了查看 Lambda 方法之间的不同结果,我将 .ToList() 放在它们之间。
回答by ocram88
The debug run on function called with lambda expression if you use ToList().
如果使用 ToList(),则调试运行在使用 lambda 表达式调用的函数上。
Example
例子
bool aFunction(int x) { return x < 10; }
var l = new List<int>() { 5, 6, 11 }.Where(el => aFunction(el)).ToList();

