title: Using OrElse in a LINQ Statement
body: <p>I have the following query:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Dim query = From m In Me.DataWorkspace.Products_dbData.MaterialList _
              Where m.ProductModel = Me.lblMyModel _
              And ((m.MaterialID = MyParam) OrElse (m.MaterialID.Contains(MyParam))) _
              Select m _
              Order By m.SortNumber
  </code></pre>
  
  <p>An excerpt of my data looks like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>ProductModel        MaterialID           SortNumber
  ------------        -------------        ----------
  111                 ABC_Material1        NULL
  111                 Material1            0
  111                 XYZ_Material1        0
  111                 Material2_J          0
  111                 Material2_K          1
  111                 Material2_L          2
  </code></pre>
  
  <p>I have two scenarios that I would like to use the same query for.</p>
  
  <p><strong>Scenario 1:</strong></p>
  
  <p><code>MyParam = "Material1"</code></p>
  
  <p><em>Desired result:</em></p>
  
  <p><code>query = "Material1"</code></p>
  
  <p>Meaning that <code>m.MaterialID = MyParam</code> matched and the <code>OrElse</code> short-circuited so <code>m.MaterialID.Contains(MyParam)</code> does not execute.</p>
  
  <p><em>Actual Result:</em></p>
  
  <p><code>query = "ABC_Material1", "Material1", "XYZ_Material1"</code></p>
  
  <p>So for some reason I don't understand, the <code>Contains</code> is executing.</p>
  
  <p><strong>Scenario 2:</strong></p>
  
  <p><code>MyParam = "Material2"</code></p>
  
  <p><em>Desired result:</em></p>
  
  <p><code>query = "Material2_J", "Material2_K", "Material2_L"</code></p>
  
  <p>Meaning that <code>m.MaterialID = MyParam</code> did not match and so <code>m.MaterialID.Contains(MyParam)</code> executes instead.</p>
  
  <p><em>Actual Result:</em></p>
  
  <p><code>query = "Material2_J", "Material2_K", "Material2_L"</code></p>
  
  <p>So this seems to work as I would expect.</p>
  
  <p><strong>Question:</strong></p>
  
  <p>Why is this not working the way I expect it to? What am I doing wrong? I'm pretty sure I'm misunderstanding the functioning of the <code>OrElse</code> operator but I can't seem to find an explanation that makes sense to me.</p>
  
  <p>Alternatives and workarounds would be appreciated.</p>