从数据库搜索并使用Entity Framework进行排序

时间:2014-09-08 15:04:17

标签: c# asp.net-mvc linq entity-framework

在我的项目中,我有一个用于搜索的文本框,现在假设有人正在搜索热披萨,那么我必须向他们展示所有包含热点或披萨的结果但是我必须订购它们而不是结果包含披萨如何首先然后显示热和然后披萨的结果 目前我正在使用此

  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;

但是问题是它为每个单词命中数据库,如何在一次命中中获得类似的结果 任何人都可以告诉我如何搜索和排序单击

-Thanks

1 个答案:

答案 0 :(得分:3)

更改

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);

编辑:删除了投影,并且包含显式搜索正在搜索的项目的字段