C#Linq在IsNull()运算符'&&'中使用Where不能应用于类型为'bool'和'int?'的操作数 [英] C# Linq use Where with IsNull() Operator '&&' cannot be applied to operands of type 'bool' and 'int?'

查看:91
本文介绍了C#Linq在IsNull()运算符'&&'中使用Where不能应用于类型为'bool'和'int?'的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am in the process of converting an old classic asp site to .NET MVC 5, Entity Framework 6.

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

I am stuck on a linq query that I cannot figure out. Here is my original SQL query:

@"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,我可以轻松地传递值.

I would pass in a value to the ISNULL(AC.AuditQuestionGroupId, '0') = ?. Now with Linq I can just pass the value in easily.

 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)

My problem is this line: where 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(..)函数.

I understand what it is telling me, but I can't figure out how to correctly get the ISNULL(..) function converted from the SQL.

推荐答案

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

This (x.AuditQuestionGroupId != null ? this.LoggedInEntity.AuditQuestionGroupId : 0) resolves to a nullable int which obviously doesn't belong as part of a boolean condition.

您的意思是:

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

这篇关于C#Linq在IsNull()运算符'&amp;&amp;'中使用Where不能应用于类型为'bool'和'int?'的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆