这段代码太可怕了,我只搜索一个属性(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;
}