5

Zastanawiam się, czy istnieje sposób na utworzenie połączonej klauzuli WHERE za pomocą tablicy int. Muszę połączyć wyniki dla całej tablicy. Mogę zrobić coś takiego:Konkatenowana klauzula Where z tablicą ciągów znaków

public ViewResult Results(int? programId, int? programYear, int? programTypeId, string toDate, string fromDate, int?[] programTypeIdList, int?[] programIdList) 
{ 
    surveyResponseRepository.Get().Any(x => x.ProgramId == programIdList); 
} 

Odpowiedz

4

Używaj Contains:

surveyResponseRepository.Get().Any(x => programIdList.Contains(x.ProgramId)); 

Mimo że dowiesz się, czy każdy wynik spełnia że kryteria.

Podejrzewam chcesz użyć Where zamiast Any:

surveyResponseRepository.Get().Where(x => programIdList.Contains(x.ProgramId)); 

Ponadto, dlaczego używasz tablicę pustych int s? Jeśli próbujesz ustawić parametr jako opcjonalny, pozostaw go jako tablicę regularnych int s i sprawdź pod kątem zerowym:

public ViewResult Results(int? programId, int? programYear, int? programTypeId, string toDate, string fromDate, int[] programTypeIdList, int[] programIdList) 
{ 
    return surveyResponseRepository.Get() 
     .Where(x => programIdList == NULL 
        || programIdList.Contains(x.ProgramId)); 

} 
1

Można to zrobić:

public ViewResult Results(int? programId, int? programYear, int? programTypeId, string toDate, string fromDate, int?[] programTypeIdList, int?[] programIdList) 
{ 
    surveyResponseRepository.Get().Where(x => programIdList.HasValue && programIdList.Value.Contains(x.ProgramId)); 
} 
Powiązane problemy