Search from database and sort using Entity Framework
In my project I have a textbox that is used to search now if someone is looking for a hot pizza then I have to show them the whole result, whether hot or pizza, but I have to order them, not the result which is that pizza comes first then shows the result hot and then pizza I am currently using this
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;
but the problem is it hits the database for every word, how to make a similar result in one hit can someone tell me how to search and sort in a single hit
-Thanks
+3
source to share
1 answer
Edit
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);
}
to
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);
Edit: remote projection and done contains search explicitly for fields when searching for project
+3
source to share