使用Linq查询存储库返回无效结果

时间:2016-11-23 18:30:56

标签: c# linq

我有一个搜索表单,在搜索目录时接受多个单词。当用户搜索每个单词长度> = 3 char的术语时,结果按预期返回。但是,如果术语包含带有< 3个字符,它会返回更多结果,这些结果对过滤器要求无效。

在我的存储库中,我有以下代码,其中传递value并且employees是DbSet。

var searchTerms = value.Split(' ')
                .Select(v => v.Trim().ToLower())
                .ToList();

var data = this.employees
                .Where(e => searchTerms.Any(s => e.fname.Contains(s)) ||
                    searchTerms.Any(s => e.lname.Contains(s)) ||
                    searchTerms.Any(s => e.dept.name.Contains(s)) ||
                    e.dept.dept_subs
                        .Where(d => searchTerms
                            .Any(s => d.name.Contains(s)))
                                .Any())
                .Where(e => e.isActive == true)
                .OrderBy(e => e.lname)
                .ThenBy(e => e.fname)
                .AsEnumerable();

所以问题仍然存在,为什么查询会破两个字符,我该如何解决呢?

更新

好。事实证明,它不限于2个字符。

结果格式如下。如果子部门为空,则它只是名称行。

LastName, FirstName
SubDepartment, Department

搜索' ali '返回

Adams, Nadiyah
Logistics, Resources Directorate

Ahmed, W*ali*ul

搜索' jr '

Brooks, *Jr*, Cecil
Customer Service Center, Program Administration Directorate 

Brown *Jr*, Peter
Document Control and Data Management, Operations Directorate

搜索' li '

Abdullah, *Li*ndsey

Abney, James
E*li*gibility, Enrollment Verification, Operations Directorate

Abrams, Charles
Customer Service Center, Program Administration Directorate

更新2

根据评论中的要求,以下是三个类:

public partial class dept
{
    ctor()
    public string name { get; set; }
    public virtual ICollection<dept_sub> dept_subs { get; set; }
    public virtual ICollection<employee> employees { get; set; }
}

public partial class dept_sub
{
    ctor()
    public string name { get; set; }
    public int dept_id { get; set; }    
    public virtual dept dept { get; set; }
    public virtual ICollection<employee> employees { get; set; }
}

public partial class employee
{
    ctor()    
    public string fname { get; set; }
    public string lname { get; set; }
    public Nullable<int> dept_id { get; set; }
    public Nullable<int> dept_sub_id { get; set; }
    public virtual dept dept { get; set; }
    public virtual dept_sub dept_sub { get; set; }

}

1 个答案:

答案 0 :(得分:0)

@KMoussa和@ Zer0让我找到了潜在的问题。我还实施了@wdosanjos推荐。

employee对象同时引用deptdept_subemployee要求dept_sub询问dept它的名字是什么,而不是围绕方法进行讨论,我改变了查询以跳过命令链(因为它已映射)并调整如下的查询。

我按如下方式调整了代码,似乎返回了更好的结果。

// trim off any whitespace on the ends
string term = value.Trim();

// remove and replace extended spaces with a single space
RegexOptions options = RegexOptions.None;
Regex regex = new Regex("[ ]{2,}", options);
term = regex.Replace(term, " ");

// break down the property into a collection of strings
List<string> searchTerms = term.Split(' ')
    .Select(v => v.Trim().ToLower())
    .ToList();

// query the dbset
IEnumerable<employee> data = this.employees
    .Where(e => searchTerms.Any(s => e.fname.Contains(s)) ||
                searchTerms.Any(s => e.lname.Contains(s)) ||
                searchTerms.Any(s => e.dept.name.Contains(s)) ||
                searchTerms.Any(s => e.dept_sub.name.Contains(s))) // new line
                // e.dept.dept_subs.Where(d => 
                //      searchTerms.Any(s => d.name.Contains(s))).Any())
    .Where(e => e.isActive == true)
    .OrderBy(e => e.lname)
    .ThenBy(e => e.fname);