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);
}
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.
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.
There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.
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 int
s? If you're trying to make the parameter optional, just leave it as an array of regular int
s 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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With