By johnc

2008-09-23 00:06:17 8 Comments

I have a Linq to objects statement

 var confirm = from l in lines.Lines 
 where (l.LineNumber == startline.LineNumber) || (l.LineNumber == endline.LineNumber) 
 select l;

The confirm object is returning an 'Object Null or Not A Reference' at at System.Linq.Enumerable.WhereListIterator`1.MoveNext()

If the result of the query was empty, it would just return an empty enumerator. I know for a fact that there are no null objects in the statement. Is it possible to step through the LINQ statement to see where it is falling over?

EDIT When I said I know for a fact that there are no null objects it turns out I was lying :[, but the question remains, though I am asuming the answer will be 'you can't really'

LINQPad is a good idea, I used it to teach myself LINQ, but I may start looking at it again as a debug / slash and burn style tool


@Sha 2019-02-20 11:01:15

While it isn't a way of debugging, I'd suggest using the following:

using (var db = new AppContext())
            db.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
            // Rest of code

You can then check the output window when debugging to see the SQL generated from your LINQ query.

@user1414213562 2016-08-07 15:19:31

It is possible to step inside the LINQ expression without setting any temporary breakpoints. You need to step into the function which evaluates the LINQ expression, e.g.:

var confirm = from l in lines.Lines 
              where (l.LineNumber == startline.LineNumber)
                    || (l.LineNumber == endline.LineNumber) 
              select l;

 confirm.ToArray(); // Press F11 ("Step into") when you reach this statement

 foreach(var o in q) // Press F11 when "in" keyword is highlighted as "next statement"
    // ...

@Michael Sorens 2010-12-10 16:24:53

I wrote a comprehensive article addressing this very question published on (LINQ Secrets Revealed: Chaining and Debugging) back in 2010:

I talk about LINQPad (as mentioned earlier by OwenP) as a great tool external to Visual Studio. Pay particular attention to its extraordinary Dump() method. You can inject this at one or more points in a LINQ chain to see your data visualized in an amazingly clean and clear fashion. Though very useful, LINQPad is external to Visual Studio. So I also present several techniques available for use within Visual Studio because sometimes it is just not practical to migrate a chunk of code over to LINQPad:

(1) Inject calls to the Dump() extension method I present in my article to allow logging. I started with Bart De Smet's Watch() method in his informative article LINQ to Objects – Debugging and added some labeling and colorization to enhance the visualization, though still it pales in comparison to LINQPad's Dump output.

(2) Bring LINQPad's visualization right into Visual Studio with Robert Ivanc's LINQPad Visualizer add-in. Not sure if it was through my prodding :-), but the couple inconveniences present when I was writing my article have now all been admirably addressed in the latest release. It has full VS2010 support and lets you examine any object you like when debugging.

(3) Embed nop statements in the middle of your LINQ chain so you can set breakpoints, as described earlier by Amazing Pete.

2016.12.01 Update

And I just wrote the sequel to the above article, titled simply LINQ Debugging and Visualization, which reveals that true LINQ debugging capability has finally arrived in Visual Studio 2015 with the about-to-be-released new feature in OzCode. @Dror's answer to this question shows a tiny glimpse of it, but I encourage you to read my new article for an in-depth "how to". (And I do not work for OzCode.:-)

@Dror Helper 2016-09-13 13:11:09

[Disclaimer: I work at OzCode]

The problem with LINQ is that it's hard to impossible to debug - even when dealing simple queries a developer is forced to refactor his/her query to a bunch of foreach loops, or use logging. LINQ debugging is supported in a soon-to-be-released version of OzCode (currently available as an Early Access Preview) and it helps developers drill into their LINQ code as well and pinpoint those hard to catch exceptions inside queries

This is what your query would look like in OzCode:Debugging LINQ exception

@Steve Steiner 2008-09-23 03:02:00

You should be able to set a breakpoint on the expression in the where clause of your LINQ statement.

In this example, put the cursor anywhere in the following section of code:

(l.LineNumber == startline.LineNumber) || (l.LineNumber == endline.LineNumber)

Then press F9 or use the menu or context menu to add the breakpoint.

When set correctly, only the above code should have the breakpoint formatting in the editor rather than the entire LINQ statement. You can also look in the breakpoints window to see.

If you've set it correctly, you will stop each time at the function that implements the above part of the query.

@ygormutti 2014-04-17 21:24:56

This may not be the answer the OP was looking for, but helped me a lot. I thought Visual Studio was unable to debug lambda expressions, mainly because when I was inserting the breakpoints (with the cursor at the wrong column) it was painting the entire line, not just the lambda I wanted to debug, but also because Quick Watch window is unable to evaluate lambda expressions.

@Chris Halcrow 2017-07-03 00:52:12

To clarify, the debugger will enter the linq query following the first reference to the variable that holds the linq query

@The amazing Pete 2009-04-03 23:15:55

Yes it is indeed possible to pause execution midway through a linq query.

Convert your linq to query style using lambda expressions and insert a Select statement that returns itself somewhere after the point in the linq that you want to debug. Some sample code will make it clearer -

        var query = dataset.Tables[0].AsEnumerable()
            .Where (i=> i.Field<string>("Project").Contains("070932.01"))
 //         .Select(i =>
 //         {return i;}
 //           )
            .Select (i=>i.Field<string>("City"));

Then uncomment the commented lines. Make sure the {return i;} is on its own line and insert a debug point there. You can put this select at any point in your long, complicated linq query.

@ljs 2008-09-23 00:15:03

From the looks of the error I would suggest you take a look at line.Lines and make sure its enumerator is implemented properly. I think it's returning a null when it shouldn't.

Oh and just make sure the line and line.Lines objects aren't null or returning nulls as well.

@Judah Gabriel Himango 2008-09-23 00:14:15

Check the exception stack trace and see the last bit of your code that executed.

@johnc 2008-09-23 00:18:02

That's how I got to System.Linq.Enumerable.WhereListIterator`1.MoveNext()

@Judah Gabriel Himango 2008-09-23 15:06:42

What's the last line of your code that executed? WhereListIterator is not your code (it's a LINQ API).

@OwenP 2008-09-23 00:09:12

I'm not sure if it's possible to debug from VS, but I find LINQPad to be quite useful. It'll let you dump the results of each part of the LINQ query.

@Dested 2009-02-13 16:14:04

Like the other side of the pillow.

Related Questions

Sponsored Content

32 Answered Questions

[SOLVED] Run/install/debug Android applications over Wi-Fi?

10 Answered Questions

[SOLVED] Group by in LINQ

  • 2011-09-06 19:44:20
  • test123
  • 1304346 View
  • 1062 Score
  • 10 Answer
  • Tags:   c# linq group-by

20 Answered Questions

[SOLVED] Dynamic LINQ OrderBy on IEnumerable<T> / IQueryable<T>

23 Answered Questions

[SOLVED] LINQ query on a DataTable

14 Answered Questions

[SOLVED] When to use .First and when to use .FirstOrDefault with LINQ?

  • 2009-06-21 19:15:29
  • Metro Smurf
  • 562326 View
  • 824 Score
  • 14 Answer
  • Tags:   c# .net linq

39 Answered Questions

[SOLVED] How do I debug Node.js applications?

12 Answered Questions

[SOLVED] LINQ Aggregate algorithm explained

  • 2011-08-18 09:51:21
  • Alexander Beletsky
  • 255848 View
  • 721 Score
  • 12 Answer
  • Tags:   c# .net linq aggregate

7 Answered Questions

[SOLVED] Multiple "order by" in LINQ

  • 2008-11-18 13:34:11
  • Sasha
  • 603975 View
  • 1582 Score
  • 7 Answer
  • Tags:   linq sql-order-by

Sponsored Content