gpt4 book ai didi

c# - 从数据库中搜索并使用 Entity Framework 进行排序

转载 作者:行者123 更新时间:2023-11-30 16:10:28 24 4
gpt4 key购买 nike

在我的项目中,我有一个用于搜索的文本框,现在假设如果有人正在搜索热比萨饼,那么我必须向他们显示所有包含热比萨饼或比萨饼的结果,但我必须订购它们,而不是包含比萨饼如何排在第一位的结果然后显示 hot 和 pizza 的结果目前我正在使用这个

  List<Project> allSearchedProject = new List<Project>();
List<string> queryList = query.ToLower().Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries).Where(q => q.Length > 3).ToList();

foreach (var q in queryList)
{
var matchedProjects = (from project in unitOfWork.ProjectRepository
.Find(p => p.IsActive && p is Project && new[] { p.Name, p.ProjectAddress.City.Name, p.ProjectAddress.Address1 }
.Any(prjt=>prjt.Contains(q)))
select project as Project).AsParallel().ToList();
allSearchedProject.AddRange(matchedProjects);
}

return allSearchedProject;

但问题是它会为每个单词访问数据库,如何一次获得相似的结果谁能告诉我如何单次搜索和排序

-谢谢

最佳答案

改变

foreach (var q in queryList)
{
var matchedProjects = (from project in unitOfWork.ProjectRepository
.Find(p => p.IsActive && p is Project && new[] { p.Name, p.ProjectAddress.City.Name, p.ProjectAddress.Address1 }
.Any(prjt=>prjt.Contains(q)))
select project as Project).AsParallel().ToList();
allSearchedProject.AddRange(matchedProjects);
}

var matchedProjects = (from project in unitOfWork.ProjectRepository
where project.IsActive
&& project is Project
&& (queryList.Contains(project.Name)
|| queryList.Contains(project.ProjectAddress.City.Name)
|| queryList.Contains(project.ProjectAddress.Address1))
select project as Project).AsParallel().ToList();
allSearchedProject.AddRange(matchedProjects);

编辑:删除了投影并使包含对正在搜索的项目中的字段进行显式搜索

关于c# - 从数据库中搜索并使用 Entity Framework 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25727328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com