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

另外,你为什么要使用一个可以为空int的数组?如果您试图使参数成为可选参数,只需将其保留为常规ints 数组并检查是否为空:

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 回答