0

Sitecore 9.3

I am trying to use 'predicates' to get a subset of data.

When I have the code snippet as:

using (var contextA = selectedIndex.CreateSearchContext())
{
 var predicate = PredicateBuilder.True<SearchResultItem>();
 predicate = predicate.And<SearchResultItem>(i => i.TemplateId == unversionedImageTemplateId || i.TemplateId == jpegTemplateId);
                       
 var queryable = contextA.GetQueryable<SearchResultItem>();
 var resultsList = queryable.Where(predicate).Select(i => i.GetItem());
}

But when I put another condition on the basis of the path in addition to the above condition, I do not get any item in result. I have checked in Sitecore that I have multiple such items.

 using (var contextA = selectedIndex.CreateSearchContext())
   {
     var predicate = PredicateBuilder.True<SearchResultItem>();
     predicate = predicate.And<SearchResultItem>(i => i.TemplateId == unversionedImageTemplateId || i.TemplateId == jpegTemplateId);

     predicate = predicate.And<SearchResultItem>(i => i.Paths.Contains(mediaLibraryId));
             
     var queryable = contextA.GetQueryable<SearchResultItem>();
     var resultsList = queryable.Where(predicate).Select(i => i.GetItem());
}
    

I am trying to add more condition on the basis of a checkbox selected or not. Please help me to understand where I am going wrong.

  • I cannot see anything wrong with your code. Check Solr query created by your code and check on Solr directly what's causing your issues. – Marek Musielak Oct 11 '20 at 13:46
  • As @MarekMusielak said, the Predicate conditions seems correct. Could you please check the Search log and find the query that is getting created. And then directly query from your solr admin panel. It would provide you where you had gone wrong. – Sreekrishnan Oct 11 '20 at 15:40
  • Seems all ok to me. Did you try re-indexing and see? – Debabrata Biswas Oct 12 '20 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.