سؤال

How can I search for multiple words with Sitecore 7 ? I tried with Contains for every word but doesn't work fine and I think performance it's not really good.

هل كانت مفيدة؟

المحلول

you can user PredicateBuilder for this issue :

Code will be something like:

 Queryable<SearchItem> SearchText(List<string> keywords, IQueryable<SearchItem> itemList)
    {
        var predicate = PredicateBuilder.True<SearchItem>();

        foreach (string keyword in keywords)
        {
            predicate = predicate.And(i => i.FieldOne.Contains(keyword) || i.FieldTwo.Contains(keyword) || i.FieldThree.Contains(keyword) || i.Fieldyyy.Contains(keyword));
        }
        return itemList.Where(predicate);
    }

About predicateBuilder you can find here : Dynamic query using predicate builder

نصائح أخرى

Performance can get better if you use the ToList(); on the IQueryable only when you have filtered out all items (using LINQ statements) you need from the IQueryable. When you call .ToList(); the query will be executed.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top