Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 861
evanmcdonnal Avatar asked Sep 04 '13 18:09

evanmcdonnal


People also ask

How do I get SQL query from LINQ in Visual Studio?

You can get same generated SQL query manually by calling ToString: string sql = committeeMember. ToString();

When the LINQ query is executed?

LINQ queries are always executed when the query variable is iterated over, not when the query variable is created. This is called deferred execution. You can also force a query to execute immediately, which is useful for caching query results. This is described later in this topic.

How do I view quick watch in Visual Studio?

It's available from Debug | Windows | Watch | Watch 1 or Ctrl + Alt + W + 1. There are 4 watch windows in Visual Studio, which you can use in different contexts (Watch 1, Watch 2, etc.).


2 Answers

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!

like image 64
Codeman Avatar answered Sep 18 '22 12:09

Codeman


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.

like image 32
Seb T. Avatar answered Sep 21 '22 12:09

Seb T.