5

Ich frage mich, ob es eine Möglichkeit gibt, eine verkettete WHERE-Klausel mit einem Array von int zu erstellen. Ich muss die Ergebnisse für das gesamte Array kombiniert bekommen. Kann ich so etwas wie:Konkatenierte Where-Klausel mit Array von Strings

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

Antwort

4

Verwendung Contains:

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

Obwohl das wird Ihnen sagen, wenn kein Ergebnis, dass die Kriterien erfüllt.

Ich vermute, Sie Where statt Any verwenden möchten:

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

Auch, warum Sie eine Reihe von Nullable-int s verwenden? Wenn Sie versuchen, den Parameter optional zu machen, lassen Sie ihn einfach als ein Array von regulären int s und überprüfen Sie für null:

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

Sie dies tun können:

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)); 
}