3

您能否指导我如何在 Razor 视图引擎中设置断点来测试我的 lambda 表达式?

例如,我有以下代码:

@(Html.DropDownList("Condition4", 
                    new SelectList(Model
                                   .Conditions
                                   .Where(c => 
                                          c.TxCondition.TxConditionTypeId == Model.ConditionTypes.Single
                                          ct => ct.TxConditionType.ConditionTypeCode == "Region")
                                    .TxConditionType
                                    .TxConditionTypeId), 
                    "TxCondition.TxConditionId", 
                    "ConditionTitle",
                    Model.SearchCondition.Condition4), 
    "All"))

在断点上,我尝试使用以下代码测试以下代码,"Quick Watch Windows" 但错误是“表达式不能包含 lambda 表达式”

你能指导我如何在 MVC Razor 视图中测试 lambda 表达式吗?

非常感谢您的时间和帮助。

Model.Conditions.Where(c => c.TxCondition.TxConditionTypeId == 1)
4

2 回答 2

2

Debugging and Lambda is always tricky to deal with.

A user asked this question: Visual Studio debugging "quick watch" tool and lambda expressions and it was explained that anonymous functions are actually very complex and require a lot of work on the compiler's part. Therefore you can't really put them into the quick watch or similar.

I can't really solve your problem, but I'd like to suggest a slightly different approach.

In MVC views are supposed to be stupid; they should really be "doing stuff". What I mean is that they shouldn't really concern themselves with creating variables, performing logic, selecting or instantiating objects and so on. Instead the should simply take objects that are given to it and attempt to display them.

This forces you to put all of those things elsewhere in your codebase. Appropriate usage of good architecture, layering, and separation of concerns will help you organise things, including business logic. Furthermore I'd suggest that, when writing logic using Lambda and if that Lambda is a little complex, divide the components into pieces so that it's easier to debug and step through.

ICollection<object> filter1 = someCollection.Where(x => x.IsAvailable);
object myObject = filter1.SingleOrDefault(x => x.SomeString = "aValue").Distinct();
于 2013-11-11T23:31:35.410 回答
1

您可以分离您的 Lamba 表达式以检查它(可能不是确切的 Razor 语法):

var conditionTypeId = Model
                        .ConditionTypes
                        .Single(ct => ct.TxConditionType.ConditionTypeCode == "Region")
                        .TxConditionType
                        .TxConditionTypeId;
var selectListContent = Model
                            .Conditions
                            .Where(c => c.TxCondition.TxConditionTypeId == conditionTypeId)
                            .ToList();

@(Html.DropDownList("Condition4", 
                  new SelectList(selectListContent, "TxCondition.TxConditionId", "ConditionTitle",Model.SearchCondition.Condition4), 
                  "All"))

看一下.ToList()afterWhere语句,这样可以在调试时查看结果列表的内容。除此之外,这将为您的代码增加一些可读性(其他开发人员会感谢您,以及您自己的未来)。保存conditionTypeId在一个单独的变量中将评估一次。

于 2013-11-11T23:27:35.780 回答