title: Why can't I compare two fields in a search predicate in Sitecore 7.5?
body: <p>I am trying to build a search predicate in code that compares two fields in Sitecore and I am getting a strange error message.  Basically I have two date fields on each content item - FirstPublishDate (the date that the content item was first published) and LastPublishDate (the last date that the content item was published).  I would like to find all content items where the LastPublishDate falls within a certain date range AND where the LastPublishDate does not equal the FirstPublishDate.  Using Linq here is my method for generating the predicate...</p>
  
  <pre><code>    protected Expression&lt;Func&lt;T, Boolean&gt;&gt; getDateFacetPredicate&lt;T&gt;() where T : MySearchResultItem
      {
          var predicate = PredicateBuilder.True&lt;T&gt;();
  
          foreach (var facet in myFacetCategories)
          {
              var dateTo = System.DateTime.Now;
              var dateFrom = dateTo.AddDays(facet.Value*-1);
  
              predicate = predicate.And(i =&gt; i.LastPublishDate.Between(dateFrom, dateTo, Inclusion.Both)).And(j =&gt; j.LastPublishDate != j.FirstPublishDate);
          }
          return predicate;
      }
  </code></pre>
  
  <p>Then I use this predicate in my general site search code to perform the search as follows:  the above predicate gets passed in to this method as the "additionalWhere" parameter.</p>
  
  <pre><code>    public static SearchResults&lt;T&gt; GeneralSearch&lt;T&gt;(string searchText, ISearchIndex index, int currentPage = 0, int pageSize = 20, string language = "", IEnumerable&lt;string&gt; additionalFields = null,
          Expression&lt;Func&lt;T, Boolean&gt;&gt; additionalWhere = null, Expression&lt;Func&lt;T, Boolean&gt;&gt; additionalFilter = null, IEnumerable&lt;string&gt; facets = null,
          Expression&lt;Func&lt;T, Boolean&gt;&gt; facetFilter = null, string sortField = null, SortDirection sortDirection = SortDirection.Ascending) where T : SearchResultItem {
  
          using (var context = index.CreateSearchContext()) {
              var query = context.GetQueryable&lt;T&gt;();
              if (!string.IsNullOrWhiteSpace(searchText)) {
                  var keywordPred = PredicateBuilder.True&lt;T&gt;();
  
                  // take into account escaping of special characters and working around Sitecore limitation with Contains and Equals methods                 
                  var isSpecialMatch = Regex.IsMatch(searchText, "[" + specialSOLRChars + "]");
                  if (isSpecialMatch) {
                      var wildcardText = string.Format("\"*{0}*\"", Regex.Replace(searchText, "([" + specialSOLRChars + "])", @"\$1"));
                      wildcardText = wildcardText.Replace(" ", "*");
                      keywordPred = keywordPred.Or(i =&gt; i.Content.MatchWildcard(wildcardText)).Or(i =&gt; i.Name.MatchWildcard(wildcardText));
                  }
                  else {
                      keywordPred = keywordPred.Or(i =&gt; i.Content.Contains(searchText)).Or(i =&gt; i.Name.Contains(searchText));
                  }
  
                  if (additionalFields != null &amp;&amp; additionalFields.Any()) {
                      keywordPred = additionalFields.Aggregate(keywordPred, (current, field) =&gt; current.Or(i =&gt; i[field].Equals(searchText)));
                  }
                  //query = query.Where(i =&gt; (i.Content.Contains(searchText) || i.Name.Contains(searchText)));    // more explicit call to check the content or item name for our term
                  query = query.Where(keywordPred);
              }
  
              if (language == string.Empty) {
                  language = Sitecore.Context.Language.ToString();
              }
  
              if (language != null) {
                  query = query.Filter(i =&gt; i.Language.Equals(language));
              }
  
              query = query.Page(currentPage, pageSize);
  
              if (additionalWhere != null) {
                  query = query.Where(additionalWhere);
              }
  
              if (additionalFilter != null) {
                  query = query.Filter(additionalFilter);
              }
  
              query = query.ApplySecurityFilter();
  
              FacetResults resultFacets = null;
              if (facets != null &amp;&amp; facets.Any()) {
                  resultFacets = facets.Aggregate(query, (current, fname) =&gt; current.FacetOn(i =&gt; i[fname])).GetFacets();
              }
              // calling this before applying facetFilter should allow us to get a total facet set
              // instead of just those related to the current result set
              // var resultFacets = query.GetFacets();    
  
              // apply after getting facets for more complete facet list
              if (facetFilter != null) {
                  query = query.Where(facetFilter);
              }
  
              if (sortField != null)
              {
                  if (sortDirection == SortDirection.Ascending)
                  {
                      query = query.OrderBy(x =&gt; x[sortField]);
                  }
                  else
                  {
                      query = query.OrderByDescending(x =&gt; x[sortField]);
                  }
              }
  
              var results = query.GetResults();   // this enumerates the actual results
              return new SearchResults&lt;T&gt;(results.Hits, results.TotalSearchResults, resultFacets);
          }
      }
  </code></pre>
  
  <p>When I try this I get the following error message:</p>
  
  <pre><code>Server Error in '/' Application.
  
  No constant node in query node of type:     'Sitecore.ContentSearch.Linq.Nodes.EqualNode'. Left:     'Sitecore.ContentSearch.Linq.Nodes.FieldNode'.  Right:     'Sitecore.ContentSearch.Linq.Nodes.FieldNode'.
  
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  
  Exception Details: System.NotSupportedException: No constant node in query node of type: 'Sitecore.ContentSearch.Linq.Nodes.EqualNode'. Left: 'Sitecore.ContentSearch.Linq.Nodes.FieldNode'.  Right: 'Sitecore.ContentSearch.Linq.Nodes.FieldNode'.
  
  Source Error: 
  
  
  Line 548:               FacetResults resultFacets = null;
  Line 549:               if (facets != null &amp;&amp; facets.Any()) {
  Line 550:                   resultFacets = facets.Aggregate(query, (current, fname) =&gt; current.FacetOn(i =&gt; i[fname])).GetFacets();
  Line 551:               }
  Line 552:               // calling this before applying facetFilter should allow us to get a total facet set
  </code></pre>
  
  <p>From what I can understand about the error message it seems to not like that I am trying to compare two different fields to each other instead of comparing a field to a constant.  The other odd thing is that the error seems to be pointing to a line of code that has to do with aggregating facets.  I did a Google search and came up with absolutely nothing relating to this error.  Any ideas?</p>
  
  <p>Thanks,
  Corey</p>