3
votes

I'm having problem with querying IEnumerable computed index field. Im using Sitecore 7.2 upd2, Lucene, ContentSearch and PredicateBuilder.

I'm trying to query product prices which are available under products section. There is some heavy Logic to find available products so I decided to put all available product prices in computed field. Unfortuantelly it looks like I'm unable to query prices list with PredicateBuilder.

My query looks like this:

predicate = predicate.And(p => p.Prices.Any(x => x >= priceFrom && x <= priceTo));

field configuration in index config:

<field fieldName="Prices"  storageType="YES" indexType="TOKENIZED"    vectorType="NO" boost="1f" type="System.Collections.Generic.IEnumerable`1[System.Int32]" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" />

and that's my error:

Invalid Method Call Argument Type: Field - FieldNode - Field: prices -     System.Collections.Generic.IEnumerable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]. Only constant arguments is supported.

Any Ideas?

2

2 Answers

3
votes

The error stems from parameter of the Any() method call.

Sitecore Content Search LINQ has some limitations. One of them is that methods only accept "constant expressions" (objects) as parameters. You are passing a "lamda expression" as a parameter for the Any method.

1
votes

I would suggest indexing both the min and max price for each product as separate computed fields (decimal) in the index.

This would greatly simplify your query:

var results = context.GetQueryable<ProductSearchResultItem>
    .Where(p => p.MinPrice >= myPrice)
    .Where(p => p.MaxPrice <= myPrice)
    .GetResults();