5

dizesiyle Bir dizi bir dizi kullanarak birleştirilmiş WHERE yan tümcesi oluşturmanın bir yolu olup olmadığını merak ediyorum. Komple dizi için sonuçları elde etmem gerekiyor. Bunu yapabilecekBirleştirilen dizeler dizisi

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

cevap

4

Kullanım Contains: Ben böyle bir şey yapabilir miyim

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

HERHANGİ sonuç kriterlerine olduğunu karşılıyorsa söyleyecektir rağmen.

Sana Any yerine Where kullanmak istediğiniz şüpheli:

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

, neden null int s bir dizi kullanıyorsunuz? Opsiyonel parametreyi yapmaya çalışıyorsanız, sadece düzenli int s bir dizi olarak bırakın ve boş olup olmadığını kontrol:

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