View result of LINQ query in watch/debugger

前端 未结 4 1177
走了就别回头了
走了就别回头了 2021-01-04 07:07

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 expre

相关标签:
4条回答
  • 2021-01-04 07:33

    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.

    0 讨论(0)
  • 2021-01-04 07:40

    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.

    0 讨论(0)
  • 2021-01-04 07:40

    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. enter image description here

    Within that, I can expand ever further and see the data inside: enter image description here

    0 讨论(0)
  • 2021-01-04 08:00

    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!

    0 讨论(0)
提交回复
热议问题