5

intの配列を使用して連結された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

2 に答える 2

4

使用Contains

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

ただし、結果がその基準を満たしているかどうかはわかります。

Where代わりに使用したいと思いますAny

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

また、なぜnull許容型の配列を使用しているintのですか?パラメータをオプションにしようとしている場合は、通常intのsの配列のままにして、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));

}
于 2012-10-08T21:43:23.713 に答える
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)); 
}
于 2012-10-08T21:42:55.933 に答える