c# - 如何调试 Linq Lambda 表达式?

标签 c# linq entity-framework debugging lambda

我正在使用 Entity Framework 和 Linq to Entitites。

我想知道在 Visual Studio 2012 中是否有任何方法可以逐步调试这段代码。 放置断点的瞬间,光标越过断点,但没有进入。

我更感兴趣的是 x.e 的值...而不是生成的 sql。

注意:我可以使用其他工具或 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
                     });

最佳答案

请务必阅读有关此问题的官方 MSDN 文档:

Debugging LINQ

请在 Visual Studio 的用户意见页面上为这个建议投票:

Debug Lambda expressions

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

这是在我当前的 Visual Studio 2013 中执行的操作:

enter image description here

如您所见,它运行良好,并允许我们查看正在测试的给定属性的值。这肯定是一个很棒的工具/救生员! :)

关于c# - 如何调试 Linq Lambda 表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17231101/

相关文章:

c# - winforms datagridview计算字段更改事件

linq - 尝试使用表达式树过滤 Nullable 类型

entity-framework - 来自另一个项目的 Entity Framework 连接字符串引用

c# - EF6 代码优先 "Foreign Key Multiplicity is not valid in Role"

.net - 使用 Entity Framework 将 SQL Server 数据库导出到 Access

c# - MVC 中的倒数计时器

c# - 如何将指针从 C# 传递到 DLL 中的 native 函数?

c# - 按钮中“(”后的符号在 wpf [zh-cn] 中的 c sharp 中表现不同

c# - 多次使用 Include() 时 Entity Framework 代码很慢

sql-server - LINQ to SQL 数据上下文日志不显示 WHERE 子句