title: Dynamic LINQ Multiple Where Clause
body: <p>Still really struggling with this and appear to be going round in circles.</p>
  
  <p>I have the following code that is driving me nuts. It should populate a list of items to be used in an autocomplete text box:</p>
  
  <pre><code>public string[] GetAutoComplete(string prefixText, int count)
      {
              string memberid = HttpContext.Current.Session["MemberID"].ToString(); 
              string locationid = HttpContext.Current.Session["LocationID"].ToString();
              string inhouse = HttpContext.Current.Session["Inhouse"].ToString();
              string supplier = HttpContext.Current.Session["Supplier"].ToString();
              string groupw = HttpContext.Current.Session["Group"].ToString();
              string external = HttpContext.Current.Session["External"].ToString();
  
              MyEnts autocomplete = new MyEnts();
  
              var r = from p in autocomplete.tblAutoCompletes
                          where p.MemberId == memberid &amp;&amp; p.LocationId == locationid &amp;&amp; p.ACItem.Contains(prefixText)
                          select p.ACItem;
  
              if (inhouse == "Inhouse")
                  r = r.Where(p =&gt; p == inhouse);
  
              if (supplier == "Supplier")
                  r = r.Where(p =&gt; p == supplier);
  
              if (groupw == "Group")
                  r = r.Where(p =&gt; p == groupw);
  
              if (external == "External")
                  r = r.Where(p =&gt; p == external);
  
              r.OrderBy(p =&gt; p);
  
              return r.ToArray();
  </code></pre>
  
  <p>What I am trying to retrieve with the dynamic where clause along the lines of the following.</p>
  
  <p>Should inhouse = "Inhouse", then the list of items should include the word "Inhouse". If inhouse != "Inhouse", the word "Inhouse" should be excluded from the list.</p>
  
  <p>This same logic should then be applied across the different where clauses i.e. Supplier, Group, External.</p>
  
  <p>I genuinely have tried lots of different methods but I cannot for the life of me get the thing to work and it's frustrating me somewhat.</p>
  
  <p>If anyone can suggest a way of doing this, you will either get a big kiss or a big frosty beer should our paths ever cross. </p>