简体   繁体   中英

View result of LINQ query in watch/debugger

Is there a way you can view the result of a LINQ query in Visual Studio 2010? If you add the query as a watch expression it will say "Expression cannot contain lambda expressions".

In some test code I'm aggregating the totals of a bunch of metrics for some number of children and comparing the sum to their parents value for the same metric (deep nested if-else). All my queries are in the if statements. How can I view these values without just assigning the result to a local variable? Assignment doubles my line count and aside from debugging here provides no benefit. Does anyone have a work around they use to view the results of LINQ queries in the debugger?

You cannot currently use lambda expressions in the watch list in Visual Studio.

There are a couple of things you can do:

  1. Create a method that calls the desired lambda, then put that method call in your watch statement.

  2. Set the desired lambda expression to a variable, then look at the contents of that variable. Be aware that this will enumerate through the expression, and may cause side effects.

I would imagine this is on the list of feature requests for VS, but MSFT has not done it yet. Hopefully this helps in the meantime.

EDIT:

GOOD NEWS! You can now evaluate lambdas in Visual Studio 2017. Huzzah!

I am digging out this old thread for the ones which are not lucky enough to use VS 2015 yet and which are still suffering from this missing feature in the previous version of VS.

It is a bit painful to have to split code just for sake of debugging.

An alternative I like to use for Where queries is : DynamicQueryable .

Let say you have a query :

myClass.Records.Where(rec => rec.Country.Code == "FRA")

Then using DynamicQueryable you can enter a watch statement which would look like:

System.Linq.Dynamic.DynamicQueryable.Where(myClass.Records.AsQueryable(), "Country.Code == \"FRA\"").ToList()

It is fairly easy to write (again for Where queries), and as this is a watch statement, quite quick to update and useful for debug purpose. Think about always adding a ToList() or ToArray() to apply the projection in your watch statement automatically.

For complex Select statements, I guess it would not be that handy, but may still help.

I would also recommend to use a tool named OzCode . Last version contains LINQ debug feature which is quite awesome . You can follow the state of the collection being modified at each level of the LINQ statement.

In Visual Studio 2015 you'll be able to debug lambda expressions (it's Preview at the time of writing). You'll be able to add watches with lambda expressions etc.

Expression Evaluator had to be rewritten, so many features are missing: remote debugging ASP.NET, declaring variables in Immediate window, inspecting dynamic variables etc. Also lambda expressions that require calls to native functions aren't currently supported. All features will be finished when VS2015 is released.

I'm not sure if this is what you mean (I may be misunderstanding) but I can see the results of my LINQ query by looking in the Locals window and expanding the Results view of my query variable. 在此输入图像描述

Within that, I can expand ever further and see the data inside: 在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM