Sitecore Asked by Arpana Mittal on August 23, 2021
I have two IQueryables that i need to merge. This is my code:
such that all results having region name “UK” will appear on top while all results having region other than UK will appear after search results of “UK”
When I am trying to run the same getting this issue :
Please let me know if there is any other query or way which I should try to get expected output
Below is the screenshot of my updated code :
But this code is not working working as while building the same I am facing this error: cannot convert from ‘System.Collections.Generic.List>’ to ‘Sitecore.ContentSearch.Linq.SearchResults’
This issue is occuring while I am trying to pass finalList in funtion MapResults()(Declaration of MapResults funtion is as : IEnumerable MapResults(SearchResults hits))
Requirement for the same is that I want to get all job results base on UK region on top and then rest of the job results after that.
Please let me know if there is any suggestion on the same.
I recently implemented this type of requirement in Commerce and same can be used here also.
Use the first predicate to get UK results
query1 = query.Where(i => i.region_names.Contains("UK"));
var results = query1.GetResults();
Use the second predicate to get non UK results
query2 = query.Where(i => !i.region_names.Contains("UK"));
var results = query2.GetResults();
Finally perform Union of the results
var results3 = results1.Union(results2);
SearchResults<SearchResultItem> fianlResults = new SearchResults<SearchResultItem>(results3, results3.Count());
This should give you the expected output. Please let me know if you face any issues
You can refer to my blog here for more details : https://codeandlearnspot.wordpress.com/2020/04/05/sitecore-solr-search-improve-relevancy-boosting-keyword-splitting/
Answered by Sreekrishnan on August 23, 2021
As specified by the documentation found here https://doc.sitecore.com/developers/90/sitecore-experience-manager/en/linq-to-sitecore.html, the concat method is not supported.
You can rather do a single query where the region_names is not empty and select the items you want from the result.
query = query.Where(i => !string.IsNullOrEmpty(i.region_names));
var results = query.GetResults();
var finalResult = results.Hits.Where(i => i.Document.region_names.Contains("UK")).ToList();
finalResult.AddRange(results.Hits.Where(i => !i.Document.region_names.Contains("UK")).ToList());
Update: For pagination feature you can use skip and take operations on the finalResult list.
var newList = finalResult.Skip(x).Take(y);
Answered by adarsh on August 23, 2021
LINQ to Sitecore provides access to search the indexes, using standard LINQ queries in the same way that other LINQ providers, such as LINQ to SQL and LINQ to Objects, work. It uses the standard IQueryable interface and supports most of the available operations. But the LINQ layer does not implement all IQueryable methods. below methods are not supported:
If these methods are called, a NotSupportedException or InvalidOperationException exception is thrown at runtime. Based on your requirement you may need to fire two queries or need to perform logic after getting the results.
Answered by Mahendra Shekhawat on August 23, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP