Reputation: 2509
Can you please guide me how can I test my lambda expressions inside Razor view engine setting a break point?
For example, I have below code:
@(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"))
On break point I tried testing the following code using "Quick Watch Windows"
but the error was "Expression cannot contain lambda expressions"
Can you please guide me how to test lambda expressions in MVC Razor view ?
Thank you so much for your time and help.
Model.Conditions.Where(c => c.TxCondition.TxConditionTypeId == 1)
Upvotes: 3
Views: 2210
Reputation: 16388
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();
Upvotes: 2
Reputation: 2551
You can dissociate your lamba expression in order to inspect it (may not be the exact Razor syntax):
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"))
Take a look at the .ToList()
after the Where
statement, this way you can inspect the content of the result list when debugging. Besides this will add some readability to your code (other developers will thank you, as well as your future-yourself).
Keeping conditionTypeId
in a separate variable will evaluate once.
Upvotes: 1