5

I am wondering if there is a way to create a concatenated WHERE clause using an array of int. I need to get the results for the entire array combined. Can I do something like:

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

2 Answers 2

4

Use Contains:

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

Though that will tell you if ANY result meets that criteria.

I suspect you want to use Where instead of Any:

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

Also, why are you using an array of nullable ints? If you're trying to make the parameter optional, just leave it as an array of regular ints and check for 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));

}
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.