Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenated Where clause with array of strings

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);
}
like image 306
user547794 Avatar asked Oct 08 '12 21:10

user547794


People also ask

How do you concatenate strings in an array?

The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

Can you use += for string concatenation?

Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings. In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class.

What are the 2 methods used for string concatenation?

There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.


1 Answers

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

}
like image 115
D Stanley Avatar answered Sep 30 '22 09:09

D Stanley