Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework Using Predicates Issue
    text
    copied!<p>I am using predicates to refine search options and am getting two different results when i run what looks to be the same query.</p> <p>This one is correct and returns the expected results:(hard coded strings) </p> <pre><code>_predicate = PredicateBuilder.False&lt;TBLDESIGN&gt;(); _predicate = _predicate.Or(a =&gt; a.IS_APPROVED == true &amp; a.ACTIVE == true &amp; a.DATE_APPROVED &gt; beginDate &amp; (a.KEYWORDS.Contains("red") || a.NAME.Contains("red"))); _predicate = _predicate.Or(a =&gt; a.IS_APPROVED == true &amp; a.ACTIVE == true &amp; a.DATE_APPROVED &gt; beginDate &amp; (a.KEYWORDS.Contains("geos") || a.NAME.Contains("geos"))); </code></pre> <p>This one only returns the results from the second predicate "geos". The first one in the loop("red") is ignored. Values "red" and "geos" are inside the loop below.</p> <pre><code>string searchTerm = ""; _predicate = PredicateBuilder.False&lt;TBLDESIGN&gt;(); for (int i = 0; i &lt; search.Count(); i++) { searchTerm = search[i].SearchTerm.Trim().ToString(); _predicate = _predicate.Or(a =&gt; a.IS_APPROVED == true &amp; a.ACTIVE == true &amp; a.DATE_APPROVED &gt; beginDate &amp; (a.KEYWORDS.Contains(searchTerm) || a.NAME.Contains(searchTerm))); } </code></pre> <p>Here is the rest of the query if that makes any difference.</p> <pre><code> var results = dbContext.TBLDESIGN .Include(s =&gt; s.LKPRICE) .Include(s =&gt; s.TBLDESIGNER) .AsExpandable().Where(_predicate) .OrderByDescending(s =&gt; s.DATE_APPROVED) .Select(s =&gt; new { s.ACTIVE, s.DATE_CREATED, s.EXPORTED, s.IMAGE_PATH, DesignId = s.ID, s.IS_APPROVED, s.TBLDESIGNER.FIRST_NAME, s.TBLDESIGNER.LAST_NAME, s.TBLDESIGNER.ALIAS, s.NAME, s.LKPRICE.PRICE, s.COMPLETED, s.DATE_APPROVED, DesignerId = s.TBLDESIGNER.ID, s.VIEW_COUNT }).ToList(); </code></pre> <p>Question is how to get the second list of predicates to return the same results as the first series. Guessing there is something extra to do when you are passing a string variable in rather than a hard coded value.</p> <p>Thanks, Billy</p> <p>Answer:</p> <pre><code>_predicate = PredicateBuilder.False&lt;TBLDESIGN&gt;(); for (int i = 0; i &lt; search.Count(); i++) { string searchTerm = search[i].SearchTerm.Trim().ToString(); _predicate = _predicate.Or(a =&gt; a.IS_APPROVED == true &amp; a.ACTIVE == true &amp; a.DATE_APPROVED &gt; beginDate &amp; (a.KEYWORDS.Contains(searchTerm) || a.NAME.Contains(searchTerm))); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload