title: Predicatebuilder group and or queries with inner outer
body: <p>I've got a pretty straightforward predicate builder query that works well. We have a list of keywords to search on, and we loop through those keywords to see if any of our object properties match up.</p>
  
  <pre><code>var predicateSearchText = PredicateBuilder.False&lt;Asset&gt;();
  List&lt;string&gt; separatedKeywords = keywords.Split(',').ToList();
  
  foreach (string s in separatedKeywords)
  {
     if (s.Length &gt; 0)
     {
        string temp = s;
        predicateSearchText = predicateSearchText.Or(m =&gt; m.Notes.Contains(temp));
        predicateSearchText = predicateSearchText.Or(m =&gt; m.Description.Contains(temp));
        predicateSearchText = predicateSearchText.Or(m =&gt; m.DetailedDescription.Contains(temp));
        predicateSearchText = predicateSearchText.Or(asset =&gt; asset.Keywords.Contains(temp));
        predicateSearchText = predicateSearchText.Or(a =&gt; a.AssetFiles.Any(x =&gt; x.FileName.Contains(temp)));
     }
  }
  </code></pre>
  
  <p>...down further I do:</p>
  
  <pre><code>var query = (from a in masterQuery.Where(predicateSearchText);
  </code></pre>
  
  <p>and I get what I need - filter to only those with the keywords for which I've searched.</p>
  
  <p>I need to change this so that I look for ALL of the keywords within a certain property of my entity. so the "notes" field must have each keyword being searched for, OR, the "description" field must have all keywords being searched for. I think this involves inner/outer predicates, but I haven't been able to come up with it. What I've tried gives me too many results, it should only give me one... the other entities don't match the criteria. is my problem obvious? My keywords (6 of them) are very unique, i'm getting back over 600 items instead of the 1...</p>
  
  <pre><code>var predicateSearchText = PredicateBuilder.False&lt;Asset&gt;();
  var notesPred = PredicateBuilder.True&lt;Asset&gt;();
  var descPred = PredicateBuilder.True&lt;Asset&gt;();
  var detdescPred = PredicateBuilder.True&lt;Asset&gt;();
  var keywordPred = PredicateBuilder.True&lt;Asset&gt;();
  var filesPred = PredicateBuilder.True&lt;Asset&gt;();
  
  foreach (string s in separatedKeywords.Where(xx=&gt;xx.Length &gt; 0))
     descPred.And(m =&gt; m.Description.Contains(s));
  
  foreach (string s in separatedKeywords.Where(xx =&gt; xx.Length &gt; 0))
     notesPred.And(m =&gt; m.Notes.Contains(s));
  
  foreach (string s in separatedKeywords.Where(xx =&gt; xx.Length &gt; 0))
     detdescPred.And(m =&gt; m.DetailedDescription.Contains(s));
  
  foreach (string s in separatedKeywords.Where(xx =&gt; xx.Length &gt; 0))
     keywordPred.And(m =&gt; m.Keywords.Contains(s));
  
  foreach (string s in separatedKeywords.Where(xx =&gt; xx.Length &gt; 0))
     filesPred.And(a =&gt; a.AssetFiles.Any(x =&gt; x.FileName.Contains(s)));
  
  predicateSearchText = predicateSearchText.Or(notesPred.Expand());
  predicateSearchText = predicateSearchText.Or(descPred.Expand());
  predicateSearchText = predicateSearchText.Or(detdescPred.Expand());
  predicateSearchText = predicateSearchText.Or(keywordPred.Expand());
  predicateSearchText = predicateSearchText.Or(filesPred.Expand());
  </code></pre>
  
  <p>Then I do my:</p>
  
  <pre><code>var query = (from a in masterQuery.Where(predicateSearchText);
  </code></pre>
  
  <p>And I get way too many results for the predicate to be correct.</p>