title: Understanding DefaultIfEmpty in LINQ
body: <p>I don't understand how <code>DefaultIfEmpty</code> method works. It is usually used to be reminiscent of left-outer join in LINQ. </p>
  
  <ul>
  <li><code>DefaultIfEmpty()</code> method must be run on a <em>collection</em>.</li>
  <li><code>DefaultIfEmpty()</code> method cannot be run on <code>null</code> collection reference.</li>
  </ul>
  
  <p>A code example I don't understand some points that</p>
  
  <ul>
  <li>Does <code>p</code>, which is after <code>into</code> keyword, refer to <code>products</code>?</li>
  <li>Is <code>ps</code> the group of product objects? I mean a sequence of sequences.</li>
  <li>If <code>DefaultIfEmpty()</code> isn't used, doesn't <strong>p</strong>, <code>from p in ps.DefaultIfEmpty()</code>, run into <code>select</code>? Why?</li>
  </ul>
  
  <p>,</p>
  
  <pre><code>#region left-outer-join
  string[] categories = {
      "Beverages",
      "Condiments",
      "Vegetables",
      "Dairy Products",
      "Seafood"
  };
  
  List&lt;Product&gt; products = GetProductList();
  
  var q = from c in categories
          join p in products on c equals p.Category into ps
          from p in ps.DefaultIfEmpty()
          select (Category: c, ProductName: p == null ? "(No products)" : p.ProductName);
  
  foreach (var v in q)
  {
      Console.WriteLine($"{v.ProductName}: {v.Category}");
  }
  #endregion
  </code></pre>
  
  <p>Code from <a href="https://github.com/dotnet/try-samples/blob/master/101-linq-samples/src/JoinOperations.cs#L92" rel="nofollow noreferrer">101 Examples of LINQ</a>.</p>