title: LINQ SelectMany and Where extension method ignoring nulls
body: <p>I have the below example code, and I am interested to know how I can make this any cleaner, possibly through better use of <code>SelectMany()</code>. At this point the <code>QuestionList</code> property will not be null. All I want is a list of <code>answerRows</code> that are not <code>null</code>, but <code>Questions</code> can sometimes be <code>null</code> too.</p>
  <pre><code>IEnumerable&lt;IQuestion&gt; questions = survey.QuestionList
                      .Where(q =&gt; q.Questions != null)
                      .SelectMany(q =&gt; q.Questions);
              
  if(questions == null)
  return null;
  
  IEnumerable&lt;IAnswerRow&gt; answerRows = questions
                      .Where(q =&gt; q.AnswerRows != null)
                      .SelectMany(q =&gt; q.AnswerRows);
  
  if(answerRows == null)
  return null;
  </code></pre>
  <p>I was interested by Jon's comment about <code>Enumerable.SelectMany</code> and Null..
  so I wanted to try my example with some fake data to more easily see where the error is, please see the below, specifically how I am using <code>SelectMany()</code> on the result of a <code>SelectMany()</code>, its clearer to me now that the problem was having to make sure you don't use <code>SelectMany()</code> on a null reference, obvious when I actually read the <code>NullReferenceException</code> name :( and finally put things together.</p>
  <p>Also while doing this, I realised that the use of <code>try { } catch() { }</code> in this example is useless and as usual Jon Skeet has the <a href="https://stackoverflow.com/questions/1998566/unhandled-exception-in-try-catch">answer</a>  :) deferred execution..</p>
  <p>so if you want to see the exception for row 2, comment out the relevant row 1 bits :P, sorry I couldn't figure out how to stop this error without re-writing the code example.</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace SelectManyExample
  {
      class Program
      {
          static void Main(string[] args)
          {
              var questionGroupList1 = new List&lt;QuestionGroup&gt;() {
                  new QuestionGroup() {
                      Questions = new List&lt;Question&gt;() {
                          new Question() {
                              AnswerRows = new List&lt;AnswerRow&gt;() {
                                  new AnswerRow(),
                                  new AnswerRow()
                              }
                          },
  
                          // empty question, causes cascading SelectMany to throw a NullReferenceException
                          null,
  
                          new Question() {
                              AnswerRows = new List&lt;AnswerRow&gt;() {
                                  new AnswerRow() {
                                      Answers = new List&lt;Answer&gt;() {
                                          new Answer(),
                                          new Answer()
                                      }
                                  }
                              }
                          }
                      }
                  }
              };
  
              var questionGroupList2 = new List&lt;QuestionGroup&gt;() {
                  null,
                  new QuestionGroup()
              };
  
              IEnumerable&lt;AnswerRow&gt; answerRows1 = null;
              IEnumerable&lt;AnswerRow&gt; answerRows2 = null;
  
              try
              {
                  answerRows1 = questionGroupList1
                      .SelectMany(q =&gt; q.Questions)
                      .SelectMany(q =&gt; q.AnswerRows);
              }
              catch(Exception e) {
                  Console.WriteLine(&quot;row 1 error = &quot; + e.Message);
              }
  
              try
              {
                  answerRows2 = questionGroupList2
                      .SelectMany(q =&gt; q.Questions)
                      .SelectMany(q =&gt; q.AnswerRows);
              }
              catch (Exception e)
              {
                  Console.WriteLine(&quot;row 2 error = &quot; + e.Message);
              }
  
  
              Console.WriteLine(&quot;row 1: &quot; + answerRows1.Count());
              Console.WriteLine(&quot;row 2: &quot; + answerRows2.Count());
              Console.ReadLine();
          }
  
  
      }
  
      public class QuestionGroup {
          public IEnumerable&lt;Question&gt; Questions { get; set; }
      }
  
      public class Question {
          public IEnumerable&lt;AnswerRow&gt; AnswerRows { get; set; }
      }
  
      public class AnswerRow {
          public IEnumerable&lt;Answer&gt; Answers { get; set; }
      }
  
      public class Answer {
          public string Name { get; set; }
      }
  }
  </code></pre>