4

I'm trying to do this:

queryable.Where(i => i["_template"] == templateId);

Where templateId is a Guid string, but it doesn't work. However, this does work:

queryable.Where(i => i["_templatename"] == templatename);

What do I need to do in order to filter by ID?

P.S. I realise I can use the typed template ID property, but I need the field name to be dynamic.

2
  • 1
    Could you try to execute your query in Luke? Link to application getopt.org/luke Nov 15, 2017 at 17:04
  • Also _template field in index stores value like c6576836910c4a3dba03c277dbd3b827 Nov 15, 2017 at 17:09

2 Answers 2

7

This probably isn't working because the value stored inside i["_template"] is the parsed GUID value from the index.

Try using:

Sitecore.ContentSearch.Utilities.IdHelper.NormalizeGuid(templateId)

This will convert any GUID to the format which is stored inside the search index.

0

Can't you do something mode along the lines of:

var predicate = PredicateBuilder.True<T>();

predicate = predicate.Where(i => i.TemplateId = templateId);

var results = queryable.Where(predicate);

There is more info on this here: https://doc.sitecore.net/sitecore%20experience%20platform/developing/developing%20with%20sitecore/search%20and%20item%20buckets/linq%20to%20sitecore

2
  • as per my question, I realise I can do this, but I need the property name to be dynamic. i.e. it's not just the template id I might want to search on, it might be a custom droptree field Nov 15, 2017 at 18:19
  • Sorry David I missed what you meant by that. Looks like you have a solution. Nov 16, 2017 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.