3

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?

Bartek KG
  • 41
  • 4

2 Answers2

3

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.

herskinduk
  • 1,187
  • 6
  • 14
  • yeah that seems to be it, I was also trying plain .Count() - with no luck. So looks like I can't really do any simple operations on IEnumerable here. – Bartek KG Oct 09 '14 at 13:42
  • Hmm.. perhaps the actual call to Any() and not the Lambda as I stated. – herskinduk Oct 09 '14 at 14:39
1

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();
Derek Hunziker
  • 12,996
  • 4
  • 57
  • 105
  • that was my initial plan:) but it fails when product prices in section are {100,200,300} and you're looking for prices between 225 and 250. In this case search shouldn't return this section. But it will if I use only max (300) and min (100) prices for comparison. Thanks Anyway! – Bartek KG Oct 09 '14 at 15:32
  • How are your prices stored and associated to products within Sitecore? Are you using the PriceMatrix field with the eCommerce module, or are they separate items that you are associating to the products, etc? – Derek Hunziker Oct 09 '14 at 17:05