使用Linq根据关键字对集合进行排序

使用Linq根据关键字对集合进行排序

问题描述:

我已经创建了一个应用程序,供用户在组织中搜索.

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.

我想对结果进行排序,以便首先(根据相关性)在OrgName中列出具有关键字的所有/所有组织,然后是在OrgName中未列出关键字的组织.

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.

以下列出了基于indexof的组织,并且仅当我使用关键字词组(而不是每个关键字)时:

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))

但这不是很合适.

是否可以对它们进行排序,以便包含输入关键字的组织首先出现在结果集合中,然后是其余记录,即关键字不在OrgName中.

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.

我相信您应该能够在orderby短语中使用条件语句来控制排序:

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);