3

In my project i have a textbox which is used for search now suppose if someone is searching hot pizza then i have to show them all result containing whether hot or pizza but i have to order them so than result containing how pizza comes first then shows result of hot and then pizza Currently I am 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 problem is this it hits database for every word, how to do get similar result in one hit can anyone tell me how to search and sort in single hit

-Thanks

1 Answer 1

3

Change

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: removed projection and made contains searching explicit for fields on project being searched

Sign up to request clarification or add additional context in comments.

8 Comments

didn't get this Line .Any(queryList.Contains(prjt=>prjt)) and it has compile error too.
Sorry, just realised a mistake in the .Any lambda, which is now corrected.
thanks i have corrected it but it is not working do i have to convert it in ToLower()
Edit made, now that I can see shape of data you were projecting into. Note projection part removed from Find
it's working fine but in Sql it search like (([Filter1].[Name1] IN (N'vishal')) AND ([Filter1].[Name1] IS NOT NULL)) but what i need is (([Filter1].[Name1] Like (N'%vishal%')) AND ([Filter1].[Name1] IS NOT NULL))
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.