实体框架代码第一个动态查询

时间:2012-12-13 12:06:18

标签: linq entity-framework

此代码非常糟糕,我只搜索一个属性(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;
    }

1 个答案:

答案 0 :(得分: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;

您还可以查看此堆栈溢出问题。这里有很多其他的想法(可能比我的好)。 Generating LinqToEntities Where statement depending on the user selection