Sitecore ContentSearch LINQ Unsupported expression node type: Parameter

I get the following exception message when I try to run the ToList method to query LINQ ContentSearch LINQ in the following code block:

Unsupported expression node type: Parameter. This could be due to ordering of   
expression statements. For example filtering after a select expression like this : 
queryable.Select(i => new { i.Id, i.Title }).Where(i => d.Title > "A"  

public virtual List<SearchResultItem> RunQuery(SearchParam param, bool showAllVersions, bool firstLoad)
{
    Assert.ArgumentNotNull(Index, "Sitecore.SharedSource.Search");
    var resultCollection = new List<SearchResultItem>();

    try
    {
        using (var context = this.Index.CreateSearchContext())
        {
            var result = context.GetQueryable<SearchResultItem>()
                                                .Where(x => HasFullText(x, param.FullTextQuery) &&
                        HasLanguage(x, param.Language) &&
                        HasRelation(x, param.RelatedIds) &&
                        HasTemplate(x, param.TemplateIds) &&
                        HasLocation(x, param.LocationIds)
                                                );
            resultCollection = result.ToList();
        }
    }
    catch (Exception exception)
    {
        Log.Error(exception.StackTrace, this);
        throw;
    }

    return resultCollection;
}

I cannot understand what is causing this problem, and I cannot reproduce the problem with standard .NET LINQ queries in a standard console application (source code at the end).

Here is the source code for the HasLanguage, HasRelation, HasTemplate and HasLocation functions. I expect this to be due to the fact that when I remove them and replace them with my implementation (where possible), I get no errors. However, when they remain inside the request, they are not even available (tried and tested debugging):

    protected bool HasRefinements(SearchResultItem pseudoResult, SafeDictionary<string> refinements)
    {
        if (refinements.Count <= 0) return false;
        foreach (var refinement in refinements)
        {
            var fieldName = refinement.Key.ToLowerInvariant();
            var fieldValue = refinement.Value;
            if (pseudoResult.GetField(fieldName).Value.Equals(IdHelper.ProcessGUIDs(fieldValue)))
            {
                return true;
            }
        }

        return false;
    }

    protected bool HasLanguage(SearchResultItem pseudoResult, string language)
    {
        if (String.IsNullOrEmpty(language)) return false;
        return pseudoResult.GetField(BuiltinFields.Language).Equals(language.ToLowerInvariant());
    }

    protected bool HasFullText(SearchResultItem pseudoResult, string searchText)
    {
        if (String.IsNullOrEmpty(searchText)) return false;
        return pseudoResult.Content.Contains(searchText);
    }

    protected bool HasId(SearchResultItem pseudoResult, string fieldName, string filter)
    {
        if (String.IsNullOrEmpty(fieldName) || String.IsNullOrEmpty(filter)) return false;
        var values = IdHelper.ParseId(filter);
        foreach (var value in values.Where(ID.IsID))
        {
            if (pseudoResult.GetField(fieldName).Value.Equals(IdHelper.ProcessGUIDs(value)))
            {
                return true;
            }
        }

        return false;
    }

    protected bool HasTemplate(SearchResultItem pseudoResult, string templateIds)
    {
        if (String.IsNullOrEmpty(templateIds)) return false;

        templateIds = IdHelper.NormalizeGuid(templateIds);
        return pseudoResult.TemplateId.ToString().Equals(templateIds);
    }

    protected bool HasLocation(SearchResultItem pseudoResult, string locationIds)
    {
        return HasId(pseudoResult, BuiltinFields.Path, locationIds);
    }

    protected bool HasRelation(SearchResultItem pseudoResult, string ids)
    {
        return HasId(pseudoResult, BuiltinFields.Links, ids);
    }  

And here is the source code of my test application using regular LINQ queries:

    static void Main(string[] args)
    {
        Program p = new Program();
        p.Process();
    }

    public void Process()
    {
        List<Boolean> flags = new List<Boolean>();
        flags.Add(true);
        flags.Add(false);
        flags.Add(false);
        flags.Add(true);
        flags.Add(false);
        bool b = true;
        try
        {
            List<Boolean> trueFlags = flags
                                        .Where<Boolean>(x => IsTrue(x, b))
                                        .ToList();
            Console.WriteLine(trueFlags.ToString());
            Console.ReadKey();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.StackTrace);
        }
    }

    public bool IsTrue(bool x, bool b)
    {
        return x ^ b;
    }  

- .

+4
1

Sitecore LINQ LINQ .NET. LINQ to SQL, LINQ.

, , Sitecore "/" . Lucene, SearchProvider SOLR Coveo. LINQ to SQL SQL. , LINQ IQueryable, IEnumerable.

IQuerable Sitecore , . Sitecore , LINQ, .

, , . PredicateBuilder

+8

Source: https://habr.com/ru/post/1527217/


All Articles