5

我想知道是否有方法使用int數組創建連接的WHERE子句。我需要得到整個陣列的結果。我可以這樣做:串聯Where子句中的字符串數組

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

回答

4

使用Contains

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

雖然這會告訴你,如果有任何結果符合這一標準。

我懷疑你想使用Where而不是Any

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

而且,爲什麼您使用可空int組成的數組?如果您試圖使參數爲可選,請將其保留爲常規int s的陣列並檢查爲空:

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

你可以這樣做:

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)); 
} 
相關問題