Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
In my project i have a text-box which is used for search now suppose if someone is searching hot pizza then i have to show them all result containing whether how or pizza but i have to order them so than result containing how pizza comes first then shows result of hot and then pizza

so i am getting query from view
then keep them in List
C#
List<string> queryList = query.ToLower().Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries).Where(q => q.Length > 3).ToList();

now if i can not use Contains for search like this
C#
var matchedProjects = (from project in unitOfWork.ProjectRepository
                                      .Find(p => p.IsActive && p is Project && new[] { p.Name, p.ProjectAddress.City.Name, p.ProjectAddress.Address1 }
                                       .Contains(queryList))


Currently I am doing this
C#
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
so can someone tell me how to search and sort them
-Thanks
Posted
Updated 8-Sep-14 4:31am
v2
Comments
Nathan Minier 8-Sep-14 10:37am    
Why are you mixing fluent and query syntax?
Vishal Pand3y 8-Sep-14 10:45am    
is there any other option ?
Nathan Minier 8-Sep-14 11:07am    
Sure, one or the other. I like fluent personally, but that's because I come from OOP and not DBA.

Also, don't query Any() when you're trying to get results, just make it part of your selector.

This is assuming that you already have a Project object defined with those fields

allSearchedProject = new List<Project>();

foreach (var q in queryList)
{
allSearchedProject.AddRange(unitOfWork.ProjectRepository
.Where(p => p.IsActive && p is Project && p.Contains(q))
.AsParallel()
.OrderBy(o => o.Name)
.ToList())
}

Assuming that unitOfWork.ProjectRepository { get; } is setup to return either IQueryable<Project> or IEnumerable<Project>.
SRS(The Coder) 9-Sep-14 0:51am    
Yes, this seems a good answer to your question.
TryAndSucceed 8-Sep-14 12:08pm    
What is the key field against which you are trying to filter? Name? or Address?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900