views:

2426

answers:

5

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

+7  A: 

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.

OwenP
Like the other side of the pillow.
Dested
+3  A: 

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

Judah Himango
That's how I got to System.Linq.Enumerable.WhereListIterator`1.MoveNext()
johnc
What's the last line of *your* code that executed? WhereListIterator is not your code (it's a LINQ API).
Judah Himango
+3  A: 

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.

kronoz
+3  A: 

You should be able to set a breakpoint on the where clause of your linq statement. You want it on the where clause, not on the definition, so play around with where the cursor is when you hit f9, and look in the breakpoints window to see. If you've set it correctly you will stop each time at the function that implements this part of the query:

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

I'll leave this editable so someone can point out precisely where to put the cursor to set the bp.

Steve Steiner
+5  A: 

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.