1

I have found using Linq to be a useful experience when querying lists, and provides succinct, readable code.

The issue I have found is when an error occurs it is very hard to debug which part of the query is failing.

Is there a way to find this out? It just highlights the whole query and returns the error.

An example is if I have a list:

class Person
{
    public IList<string> Pets
    {
        // please, don't do this at home :)
        get { throw new InvalidOperationException(); }
    }
}

Person person = new Person();

List<Person> myStrings = new List<Person>();
myStrings.Add(person);

var people = from p in myStrings
             where p.Pets.Count > 0
             select p;

Obviously checking for null is a simple solution, but for more convoluted errors that also may not be as obvious, how should we locate where in the query is failing?

Is there a Linq profiler or something?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Russell
  • 17,481
  • 23
  • 81
  • 125
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Feb 18 '11 at 01:21
  • @John, a dup, but the title is misleading. Perhaps it should be changed to "NullReferenceException -- What is it and how to avoid?" – Kirk Woll Feb 18 '11 at 01:23
  • 3
    @Kirk: It **isn't** a duplicate. Read this question. He's asking about more convoluted errors. – SLaks Feb 18 '11 at 01:23
  • 1
    @John @Kirk: This question is about debugging LINQ query expressions. The NullReferenceException is just an example. – R. Martinho Fernandes Feb 18 '11 at 01:28
  • @Slaks, (and @Martinho) I take your point -- this question is more about diagnosing and less about coding/planning for them. – Kirk Woll Feb 18 '11 at 01:28
  • @SLaks: it _is_ a duplicate: "i have a complicated expression, and I get NullReferenceException - help". – John Saunders Feb 18 '11 at 01:29
  • @Martinho: so, the complicated expressions happen to be query expressions? Doesn't change the fact that something is null. – John Saunders Feb 18 '11 at 01:30
  • 4
    The NullReferenceException was a simple example to (hopefully) illustrate my question. I hope it is not too confusing! The question is about diagnosing complex linq queries. Thanks – Russell Feb 18 '11 at 01:34
  • @Russell: then you need a better example. What other kind of "diagnosing" do you need? – John Saunders Feb 18 '11 at 02:02
  • I have changed the title to make it clear you are not just asking about NullReferenceException. – John Saunders Jan 21 '15 at 18:55

2 Answers2

4

You can set Visual Studio to break on all exceptions, and it will break as soon as the exception is thrown, inside your lambda.

This can also uncover subtle bugs; I highly recommend it.
However, for old, exception-laden code, it will be a nightmare.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
-1
Person person = new Person();
person.Pets = null;

List<Person> myStrings = new List<Person>();
myStrings.Add(person);

var people = from p in myStrings
             let pets = p.Pets
             where pets != null && pets.Count > 0
             select p;

I'm sorry if people don't think this is a duplicate. It is.

The problem is always to break down your expression, looking for something that's null that you didn't think about ahead of time. The problem is no different with LINQ queries and lambda expressions as with any other complicated expression. Break it down, put the pieces on separate lines, then find exactly which line it breaks on.


This isn't too hard in Visual Studio 2010:

enter image description here

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 3
    Thanks @John, I appreciate your point, but in that case even having multiple lines for a query, the exception is not pointed out directly. – Russell Feb 18 '11 at 01:37
  • I recommend taking out the snark about this being a duplicate; that probably explains the low score. The truth is, even if the *answer* is a duplicate, the *question* is absolutely not; the question isn't "what do I do about a NullReferenceException", it's "what do I do when the exception is buried in a Linq query". There's nothing wrong with saying "Standard debugging techniques work fine here", but as is, the unfriendly tone just takes quality away. – TheRubberDuck Jan 21 '15 at 18:49
  • I don't see "snark" in pointing out that there is nothing terribly special about LINQ queries that would make it difficult to find out where a NullReferenceException is coming from. It didn't take too much effort for me to come up with that screen shot, for instance. I'll get rid of that just the same. Note also that I changed the title to make it clear that it's a more general question than just NullReferenceException. That said, the debugging techniques are still exactly the same. – John Saunders Jan 21 '15 at 18:59