title: Dynamically Build 'where' clause in Linq
body: <p>I have a code which searches a data table for specific strings and returns back a subset of the entire datatable based on certain condition (unique id field). Sample code below which searches for "First" and "Employee" in a datatable and only returns those entries back which have same l_id value.</p>
  
  <pre><code>//Get all Id's that satisfy all conditions:            
  List&lt;int&gt; results = dtResult.AsEnumerable()
      // Get all Id's:
      .Select(dataRow =&gt; dataRow.Field&lt;int&gt;("l_id"))
      // Filter the Id's : 
      .Where(id =&gt;
          // the Id should be greater than one.
              id &gt; 1
                  // and the datatable rows with this Id should have a record with W_Text = First
              &amp;&amp; dtResult.AsEnumerable().Any(dataRow =&gt; dataRow.Field&lt;string&gt;("W_Text") == "First" &amp;&amp; dataRow.Field&lt;int&gt;("l_id") == id)
                  // and the datatable rows with this Id should have a record with W_Text = Employee
              &amp;&amp; dtResult.AsEnumerable().Any(dataRow =&gt; dataRow.Field&lt;string&gt;("W_Text") == "Employee" &amp;&amp; dataRow.Field&lt;int&gt;("l_id") == id))
              .Distinct().ToList();
  
  // Get all datatable rows filtered by the list of Id's.
  dtCopy = dtResult.AsEnumerable().Where(dataRow =&gt; results.Contains((dataRow.Field&lt;int&gt;("l_id")))).CopyToDataTable();
  </code></pre>
  
  <p>Now if I have get the search text dynamically then how do I proceed with this? 
  For. Eg, in the above code if I had to pass "First" as well as "Employee" as well as "salary" then how do i proceed? </p>
  
  <p>How can I make this generic?</p>
  
  <p>Thanks in advance for your assistance!</p>
  
  <p>EDIT - Looks like I have not been clear in my post, so let me rephrase again</p>
  
  <p>I have  a datatable which looks like this</p>
  
  <pre><code>l    |  t    |  r    |  b   |   x   |   y   |   w_text | l_id
  -------------------------------------------------------------
  70   |  314  |  141  |  328 |   1   |   5   |   First  | 4
  149  |  318  |  194  |  328 |   2   |   5   |   Employe| 4
  204  |  311  |  254  |  326 |   3   |   5   |   John   | 4
  264  |  311  |  325  |  326 |   4   |   5   |   Smith  | 4
  1924 |  310  |  2000 |  329 |   5   |   5   |   First  | 5
  70   |  341  |  109  |  355 |   1   |   6   |   step   | 5
  115  |  340  |  130  |  355 |   2   |   6   |   of     | 5
  136  |  340  |  175  |  355 |   3   |   6   |   Linq   | 5
  185  |  339  |  320  |  356 |   4   |   6   |   Last   | 6
  70   |  394  |  101  |  411 |   1   |   8   |   Employe| 6
  114  |  390  |  199  |  405 |   2   |   8   |   John   | 6
  210  |  390  |  269  |  405 |   3   |   8   |   Doe    | 6
  </code></pre>
  
  <p>The only search criteria I have on my hand is 'W_Text'.So I would want to search for say the unique phrase "First Employee".Only one l_id (in this case l_id = 4) would have both the words "First" as well as "Employee".If i search "First" separately and "Employee" separately,then I would get a bigger data set which doesnt solve my purpose. My objective is to get the below unique dataset when i search"First Employee"</p>
  
  <pre><code>l    |  t    |  r    |  b   |   x   |   y   |   w_text | l_id
  -------------------------------------------------------------
  70   |  314  |  141  |  328 |   1   |   5   |   First  | 4
  149  |  318  |  194  |  328 |   2   |   5   |   Employe| 4
  204  |  311  |  254  |  326 |   3   |   5   |   John   | 4
  264  |  311  |  325  |  326 |   4   |   5   |   Smith  | 4
  </code></pre>
  
  <p>In SQL terms, this is similar to </p>
  
  <pre><code>Select * From Table where l_id in (Select l_id from Table where W_Text in ('First','Employee') group by l_id having count(l_id) &gt; 1)
  </code></pre>
  
  <p>The above code which I have mentioned (was helped by a good Samaritan) works perfectly fine and returns me the above dataset. The problem is that it works only with "First Employee". I am having trouble with searching say "First step of Linq". the search phrase is passed to the program at runtime and can be of how many  ever words. I have tried separating the Wheres but the 'having' condition misses and which is where the entire dataset again is returned.</p>
  
  <p>Hence I request you all to kindly assist me with this problem. I am very new to Linq and am trying to work my way through. In the meantime, any help I can get would be highly appreciated. Thanks.</p>
  
  <p>---EDIT got this working using this code (with someone's help)</p>
  
  <pre><code> List&lt;string&gt; wTextFilter = new List&lt;string&gt;();
  
          foreach (string sf in strInputString.Split(' ')) //array by splitting on white space
          {
              wTextFilter.Add(sf);
          }
  
          // Get all Id's that satisfy all conditions:            
          List&lt;int&gt; results = dtResult.AsEnumerable()
              // Get all Id's:
              .Select(dataRow =&gt; dataRow.Field&lt;int&gt;("l_id"))
              // Filter the Id's : 
              .Where(id =&gt;
                  // the Id should be greater than one.
                      id &gt; 1 &amp;&amp;
                          // check if all W_Text entries has a record in the datatable with the same Id.
                      wTextFilter.All(W_Text =&gt; dtResult.AsEnumerable().Any(dataRow =&gt; dataRow.Field&lt;string&gt;("W_Text") == W_Text &amp;&amp; dataRow.Field&lt;int&gt;("l_id") == id)))
                      .Distinct().ToList();
  
          // Get all datatable rows filtered by the list of Id's.
          dtCopy = dtResult.AsEnumerable().Where(dataRow =&gt; results.Contains((dataRow.Field&lt;int&gt;("l_id")))).CopyToDataTable();      
  </code></pre>