我正在将旧的经典ASP网站转换为.NET MVC 5,实体框架6。

我陷入了我无法弄清的linq查询。
这是我原始的SQL查询:

@"SELECT DISTINCT AC.id, AC.name, ISNULL(AC.[order], 999)
        FROM tbl12AuditCategories AC
        INNER JOIN tbl12AuditQuestions AQ ON AC.id = AQ.new_categoryId
        WHERE AQ.include = 1 AND AC.include = 1 AND ISNULL(AC.AuditQuestionGroupId, '0') = ?
        ORDER BY ISNULL(AC.[order], 999), AC.name";


我将一个值传递给ISNULL(AC.AuditQuestionGroupId,'0')=?。现在,使用Linq,我可以轻松地传递值。

 PracticeConductViewModel pcvm = new PracticeConductViewModel();
            pcvm.Categories = (from x in _repository.GetAll<ReviewCategory>()
                               join y in _repository.GetAll<ReviewQuestion>()
                                on x.id equals y.CategoryId
                               orderby x.order == null ? 999 : x.order, x.name
                              where x.include == true && y.include == true && (x.AuditQuestionGroupId != null ? this.LoggedInEntity.AuditQuestionGroupId : 0)
                              select x).ToList();


我的问题是这一行:
其中x.include == true && y.include == true &&(x.AuditQuestionGroupId!= null?this.LoggedInEntity.AuditQuestionGroupId:0)

我得到错误:

Operator '&&' cannot be applied to operands of type 'bool' and 'int?'


我知道它在告诉我什么,但是我不知道如何正确地从SQL转换为ISNULL(..)函数。

最佳答案

这个(x.AuditQuestionGroupId!= null?this.LoggedInEntity.AuditQuestionGroupId:0)解析为一个可为null的int,该int显然不属于布尔条件的一部分。

您的意思是:

where x.include == true && y.include == true
   && ((x.AuditQuestionGroupId != null ? x.AuditQuestionGroupId : 0) ==  this.LoggedInEntity.AuditQuestionGroupId)

10-04 18:50