title: how do I treat null lists like empty lists in linq?
body: <p>Below is some linqpad test code. When this runs it errors because the second instance of "item" has a null list of subitems as opposed to an empty list.</p>
  
  <p>I want to treat both situations (null or empty list) in exactly the same way but I wondered if there was a cleaner way than just putting a null check on the list and initialising an empty list when there's a null.</p>
  
  <p>in other words, I could do this:</p>
  
  <pre><code>from si in (i.subitems == null ? new List&lt;item&gt;() : i.subitems)
  </code></pre>
  
  <p>but that's a little ugly and I wondered how I could improve on that?</p>
  
  <pre><code>public class item
  {
      public string itemname { get; set; }
      public List&lt;item&gt; subitems { get; set; }
  }
  
  void Main()
  {
      List&lt;item&gt; myItemList = new List&lt;item&gt;() 
      {
          new item 
          {
              itemname = "item1",
              subitems = new List&lt;item&gt;()
              {
                  new item { itemname = "subitem1" },
                  new item { itemname = "subitem2" }
              }
          },
          new item 
          {
              itemname = "item2"
          }
      };
  
      myItemList.Dump();
  
      var res = (from i in myItemList
              from si in i.subitems
              select new {i.itemname, subitemname = si.itemname}).ToList();
  
      res.Dump();
  }
  </code></pre>
  
  <p>as a bonus question, can this same linq query be represented as a lambda and treat nulls the same way?</p>
  
  <p>Cheers, Chris</p>