I've created an application were users search through organisations.
public class Organisation
{
public string OrgName { get; set; }
public string ContactName { get; set; }
public string OverviewOfServices { get; set; }
public string Address1 { get; set; }
public string Town { get; set; }
public string PostCode { get; set; }
public string Keywords { get; set; }
}
The user can enter multiple keywords. I split the keywords into an array:
string[] searchTerms = keywordphrase.Split(new[] { ' ' },StringSplitOptions.RemoveEmptyEntries);
and search the model as follows:
orglist = _UoW.OrganisationRepo.All();
orglist = (from org in orglist
where searchTerms
.All(s => org.OrgName.ToLower().Contains(s.ToLower()) ||
org.OverviewOfServices.ToLower().Contains(s.ToLower()) ||
org.ContactName.ToLower().Contains(s.ToLower()) ||
org.Address1.ToLower().Contains(s.ToLower()) ||
org.Town.ToLower().Contains(s.ToLower()) ||
org.PostCode.ToLower().Contains(s.ToLower()) ||
org.Keywords.ToLower().Contains(s.ToLower()))
select org);
I now have a list of those organisations that contain the keywords in any of the fields specified.
I want to order the results so that the organisations with any/all of the keywords listed in the OrgName come first (by relevance), then the organisations where the keywords are not listed in the OrgName come after.
The following lists orgs based on the indexof and only if I use the keywordphrase (not each keyword):
orglist = orglist.OrderBy(m => m.OrgName.StartsWith(keywordphrase))
But this is not really suitable.
Is there a way to order these so the organisations containing any of the keywords entered come first in the results collections, thenby the rest of the records, i.e.where the keywords are not in the OrgName.