0

这段代码太可怕了,我只搜索一个属性(CompanyName)。我如何动态地或无论如何更好地编写此查询?

   public List<SubContractor> GetSearchSubcontractorList()
    {
        var list = CacheObjects.Subcontractors;
        var searchItem = string.Empty;
        if (string.IsNullOrWhiteSpace(this.SearchCompanyName) == false)
        {
            var indexes = this.SearchCompanyName.IndexOfAll("*").ToList();
            if (indexes.Any() == false)
            {
                list = list.Where(x => x.CompanyName == this.SearchCompanyName).ToList();
            }

            if (indexes.Count() == 1)
            {
                if (this.SearchCompanyName.StartsWith("*"))
                {
                    searchItem = this.SearchCompanyName.Replace("*", string.Empty);
                    list = list.Where(x => x.CompanyName.EndsWith(searchItem)).ToList();
                }
                else
                {
                    searchItem = this.SearchCompanyName.Replace("*", string.Empty);
                    list = list.Where(x => x.CompanyName.StartsWith(searchItem)).ToList();
                }
            }

            if (indexes.Count() == 2)
            {
                searchItem = this.SearchCompanyName.Replace("*", string.Empty);
                list = list.Where(x => x.CompanyName.Contains(searchItem)).ToList();
            }
        }

        return list;
    }
4

1 回答 1

1

哦,对不起,我理解错了。我编辑了,查看新的解决方案。我认为您只有 4 个不同的案例需要测试,对吗?无通配符,以通配符开头,以通配符结尾,两端都有通配符。新解决方案使用延迟查询执行,以便您可以继续使用更多属性构建查询。公平警告,仍未遵守...

    var filteredSubcontractors = (from s in list
                             select s);

    if (string.IsNullOrWhiteSpace(this.SearchCompanyName) == false)
    {
        searchItem = this.SearchCompanyName.Replace("*", string.Empty);

        if (!SearchCompanyName.Contains("*"))
        {
            filteredSubcontractors = (from s in filteredSubcontractors 
                             where s.CompanyName == this.SearchCompanyName
                             select s);
        }
        else if(SearchCompanyName.StartsWith("*"))
        {      
            filteredSubcontractors = (from s in filteredSubcontractors 
                             where s.CompanyName.EndsWith(searchItem)
                             select s);
        }
        else if(SearchCompanyName.EndsWith("*"))
        {
            filteredSubcontractors = (from s in filteredSubcontractors 
                             where s.CompanyName.StartsWith(searchItem)
                             select s);
        }
        else
        {
            filteredSubcontractors = (from s in filteredSubcontractors 
                             where s.CompanyName.Contains(searchItem)
                             select s);
        }
    }

    ...
    //Repeat for as many other properties that you want to filter on
    ...

    //All the conditions that you added will not actually be evaluated 
    //until this line is executed.
    var result = filteredSubcontractors.ToList();

    return result;

您还可以查看此堆栈溢出问题。这里还有很多其他的想法(可能比我的好)。 根据用户选择生成 LinqToEntities Where 语句

于 2012-12-13T14:05:36.230 回答