0

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.

Dez79
  • 527
  • 2
  • 9
  • 25
  • You'll want to use a `Where(m => m.OrgName.StartsWith(keywordphrase))` on the list to get the orgs that match, then [`Except`](https://stackoverflow.com/questions/2404301/linq-find-differences-in-two-lists) to get the orgs that don't match, and concatenate the result. But, you should really be using a dataabse for something like this. – Ian Kemp Jan 07 '19 at 17:53
  • Which field would you call the "title"? – NetMage Jan 07 '19 at 20:21
  • @NetMage Sorry title is OrgName, ive update my question – Dez79 Jan 07 '19 at 22:40
  • What is the type of `orglist` or the signature of `OrganisationRepo.All`? Where did it come from? – NetMage Jan 07 '19 at 22:52
  • OrganisationRepo.All returns an IQueryable of Organisation, there are further queries on the search prior to this. – Dez79 Jan 08 '19 at 10:27

1 Answers1

0

I believe you should be able to use a conditional statement in the orderby phrase to control sorting:

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()))
                   orderby searchTerms.Any(s => org.OrgName.Contains(s)) ? 1 : 2
                   select org);
NetMage
  • 26,163
  • 3
  • 34
  • 55
  • This doesn't solve my query, what should be in the conditional statement!? – Dez79 Jan 08 '19 at 10:27
  • @Dez79 Do you see the conditional statement in the code I provided? What is wrong with this code? Did you try it? – NetMage Jan 08 '19 at 21:46