2012-12-13 4 views
0

이 코드는 끔찍하며 하나의 속성 (CompanyName) 만 검색합니다. 이 쿼리를 동적으로 작성하거나 어떤 경우에 더 잘 작성합니까?Entity Framework 코드 첫 번째 동적 쿼리

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

오우 죄송합니다. 편집 한 후 새로운 해결책을 확인하십시오. 네가 테스트 할 수있는 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

+0

*는 검색의 와일드 카드입니다. 그게 명확하지 않은 경우 사과드립니다. 그래서 *는 존재하지 않거나 처음이나 끝에 있거나 둘 다있을 것입니다. 그것이 내가 코드를 작성한 이유입니다. – arame3333

+0

아, 새로운 대답을 확인해보십시오. –

+0

그래도 제대로 작동하지 않더라도 더 낫습니다. StartWith와 함께 할 조건 앞에 검색 문자열이 시작되고 끝나는 지 확인해야합니다. 그러나 다른 문제는 SearchCompanyAddress와 같은 다른 모든 검색 조건에 대해 동일한 코드를 수행해야한다는 것입니다. 반복하지 않으려면이 코드를 추상화하는 방법을 알 수 없습니다. – arame3333

관련 문제