我正在尝试对 s 集合创建搜索Organisation(我正在使用 LINQ to Entities)。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; }}用户输入一些关键字,然后我想返回上面任何字段中存在所有关键字的所有Organisations 。Organisation// 1. Remove special characters and create an array of keywordsstring[] Keywords = CleanKeyWordString(model.keyword)// 2 . Get organisationsorglist = _UoW.OrganisationRepo.All();(OrganisationRepo.All返回一个IQueryableof Organisation。在此之前还有关于搜索的进一步查询)// 3. Filter results by keywordsorglist = (from org in orglist where Keywords.All(s => org.OrgName.ToLower().Contains(s) || org.OverviewOfServices.ToLower().Contains(s) || org.ContactName.Contains(s) || org.Address1.ToLower().Contains(s) || org.Town.ToLower().Contains(s) || org.PostCode.ToLower().Contains(s) || org.Keywords.ToLower().Contains(s)) orderby searchTerms.Any(s => org.OrgName.ToLower().Contains(s)) ? 1 : 2 select org);这会返回所需的结果,但是,我现在想对它们进行排序,以便首先对标题中包含关键字的记录进行排序,然后对其余的进行排序。如果不向结果添加某种排名算法,这可能吗?
1 回答
天涯尽头无女友
TA贡献1831条经验 获得超9个赞
var withRank = orglist
.Select(o =>
new {
Org = o,
Rank = o.OrgName.ToLower().Contains(s)
});
var orderedOrgList = withRank
.OrderBy(o => o.Rank)
.Select(o => o.Org);
- 1 回答
- 0 关注
- 97 浏览
添加回答
举报
0/150
提交
取消