带有字符串数组的Concatenated Where子句

use*_*794 5 c# asp.net-mvc linq-to-entities entity-framework asp.net-mvc-3

我想知道是否有办法使用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);
}
Run Code Online (Sandbox Code Playgroud)

D S*_*ley 4

使用Contains

surveyResponseRepository.Get().Any(x => programIdList.Contains(x.ProgramId));
Run Code Online (Sandbox Code Playgroud)

但这会告诉您是否有任何结果符合该标准。

我怀疑你想使用Where而不是Any

surveyResponseRepository.Get().Where(x => programIdList.Contains(x.ProgramId));
Run Code Online (Sandbox Code Playgroud)

另外,为什么要使用可空数组int?如果您尝试使参数可选,只需将其保留为常规数组int并检查是否为 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));

}
Run Code Online (Sandbox Code Playgroud)