Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LINQ in Visual Studio while debugging

Note: I am willing to download and install any extension that may be made for this type of thing. The solution does not need to come directly from standard VS functionality.

Question: Is there a way to use LINQ or any other querying method to test and view results of a List, Array, IEnumerable, etc... while stopped in the debugger?

I know that you can drop the information contained in an object using the immediate window but I want to know if there is a way that I can sort of build my required LINQ with the actual data.

Update:

This is the error I get when trying to use LINQ in the immediate window:

returnRecords.Select( x => x )

error CS1061: 'List' does not contain a definition for 'Select' and no > extension method 'Select' accepting a first argument of type 'List' could be found (are you missing a using directive or an assembly reference?)error CS1061: 'List' does not

But this works fine in code

var fu = returnRecords.Select( x => x );

Update 2:

Even something as simple as this will not work:

        List<int> abc = new List<int>();
        abc.Add(12);
        abc.Add(15);
        abc.Add(16);
        abc.Add(91);
        abc.Add(81);
        abc.Add(14);
        abc.Add(13);
        abc.Add(10);
        abc.Add(145);
        abc.Add(12);

immidiate window:

abc
Count = 9
    [0]: 12
    [1]: 15
    [2]: 16
    [3]: 91
    [4]: 81
    [5]: 14
    [6]: 13
    [7]: 10
    [8]: 145
abc.Where(x => x < 50);
error CS1061: 'List<int>' does not contain a definition for 'where' and no extension method 'where' accepting a first argument of type 'List<int>' could be found (are you missing a using directive or an assembly reference?)
like image 328
Jmaurier Avatar asked Oct 21 '16 16:10

Jmaurier


People also ask

Can we use LINQ in VB net?

LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve data from different sources and formats. It is integrated in C# or VB, thereby eliminating the mismatch between programming languages and databases, as well as providing a single querying interface for different types of data sources.

Is LINQ to SQL still used?

LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.


1 Answers

Your immediate issue is that you are missing a reference to the LINQ assembly in your code; that's why you are seeing the CS1061 error. Just add using System.Linq; to the top of your file. Then, you will be able to execute the LINQ statements in the immediate window, and you'll see, e.g.:

abc.Where(x => x < 50)
{System.Linq.Enumerable.WhereListIterator<int>}
    [0]: 12
    [1]: 15
    [2]: 16
    . . .

For more information on debugging LINQ, though, I refer you to two articles that cover it thoroughly:

  • Part 1: LINQ Secrets Revealed: Chaining and Debugging
  • Part 2: LINQ Debugging and Visualization

This is essentially a two-part series (though six years elapsed between the two parts!). The first walks through a variety of ways that you could approach debugging in LINQ, none of which are terribly satisfying, but were about as good as you could get. LINQ debugging in the early days

It is still a very useful read, to lay the groundwork of a good understanding of what is involved with debugging LINQ. Then the second article presents the about-to-be-released new capability of OzCode, a Visual Studio extension, that brings full support to LINQ debugging in situ. LINQ debugging with OzCode

like image 127
Michael Sorens Avatar answered Sep 27 '22 08:09

Michael Sorens