title: LINQ: Selecting products under a category, including sub-categories
body: <p>The code below searches for products matching a keyword <code>s</code> or being under a category matching the keyword <code>s</code>.</p>
  
  <p>It is working but tedious, I am wondering if there is a shorter way to do this?</p>
  
  <pre><code>var products = context.Products.Where(x =&gt; 
                   x.Name.Contains(s) ||
                   x.Products_Categories.Any(pc =&gt; 
                       pc.Category.Name.Contains(s) || 
                       (pc.Category.Category1 != null &amp;&amp; pc.Category.Category1.Name.Contains(s)) || 
                       (pc.Category.Category1 != null &amp;&amp; pc.Category.Category1.Category1 != null &amp;&amp; pc.Category.Category1.Category1.Name.Contains(s) || 
                       (pc.Category.Category1 != null &amp;&amp; pc.Category.Category1.Category1 != null pc.Category.Category1.Category1.Category1 != null &amp;&amp; &amp;&amp;pc.Category.Category1.Category1.Category1.Name.Contains(s))
                 );
  </code></pre>
  
  <p>If it is not obvious:</p>
  
  <p><code>Products_Categories</code> is a many-to-many relationship, a <code>Product</code> can be in one or more <code>Category</code>.</p>
  
  <p><code>Category1</code> is the parent category of the <code>Category</code>.</p>
  
  <p>Note that it is linked to a database so I cannot use functions like <code>IsUnderCategory()</code>.<br>
  I don't need Expression at the moment because this code is only used once.</p>