Apply orderby if no query just filter

Vote:
 

Hi, i am wondering if there is a elegant way to to add .orderby when there is no query string and only filters for the query. That is, the same query code should not add a orderby when there is a querystring (sort order relevance) and do a orderby when querystring is empty:

var searchResults = SearchClient.Instance.Search(Language.Swedish)
.For(querystring) 
.InField(x => x.SearchableTitle)

....

.Filter(filterForSpecialization)
.Filter(filterForProvider.filterBuilder)
....

.OrderBy(x => x.SearchableTitle) // should only be applied when querystring = ""
.GetContentResult();

Any ideas?

Regards

Magnus

#140254
Oct 14, 2015 11:27
Vote:
 

var query = SearchClient.Instance.Search<EducationEventPage>(Language.Swedish)
.For(querystring) 
.InField(x => x.SearchableTitle)

....

.Filter(filterForSpecialization)
.Filter(filterForProvider.filterBuilder)
....

if (string.IsNullOrEmptyWhitespace(querystring)) {
query = query.OrderBy(x => x.SearchableTitle);
}

var searchResult = query.GetContentResult();

#140255
Oct 14, 2015 11:56
Vote:
 

Thanks!


Magnus

#140289
Oct 15, 2015 8:03
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.