[C#]Linq to sql 中 !运算符为可以为 null 的类型为 int

标签: Linq SQLServer C#
发布时间: 2017/2/27 3:35:27
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

sql 列 ReadershipLevels - int 用 null

level是本地 int 类型的变量

&bitwise 运算符

Linq to SQL 中

 vAdvanceSearchResults = from t 
                         in vAdvanceSearchResults 
                         where (t.ReadershipLevels & level) > 0 
                         select t;

如果level = 32

以上 Linq 查询生成的 SQL 如下︰

SELECT [t2].[ReadershipLevels],[t2].[ID], [t2].[ISBN], [t2].[IsHighlighted], [t2].[Title], 
FROM (

    SELECT DISTINCT [t1].[ID], [t1].[ISBN], [t1].[IsHighlighted], [t1].[Title], [t1].[SuluAuthors], 
    FROM [dbo].[udf_SearchDWSearch]('swim', 'ALL') AS [t0]
    INNER JOIN [dbo].[DWSearch] AS [t1] ON [t0].[DWSearchID] = ([t1].[ID])
    WHERE [t1].[DatePublished] < '2016/05/27'
    ) AS [t2]
    WHERE ([t2].[ReadershipLevels] & 32) >0
    order by [t2].[ReadershipLevels]

我想要包括 not 像下面使

not ([t2].[ReadershipLevels] & 32) > 0



SELECT DISTINCT [t1].[ID], [t1].[ISBN], [t1].[IsHighlighted], [t1].[Title], [t1].[SuluAuthors], 
        FROM [dbo].[udf_SearchDWSearch]('swim', 'ALL') AS [t0]
        INNER JOIN [dbo].[DWSearch] AS [t1] ON [t0].[DWSearchID] = ([t1].[ID])
        WHERE [t1].[DatePublished] < '2016/05/27'
        ) AS [t2]
        WHERE not ([t2].[ReadershipLevels] & 32) >0
        order by [t2].[ReadershipLevels]

这工作正常。

现在我想要改变同样也在 LINQ 到 SQL

vAdvanceSearchResults = from t 
                        in vAdvanceSearchResults 
                        where ! (t.ReadershipLevels & level) > 0 
                        select t;

但上述的投掷错误-无法将运算符应用 !对操作数的类型 System.Nullable

改变了,

vAdvanceSearchResults = from t 
                        in vAdvanceSearchResults 
                        where ! (t.ReadershipLevels.Value & level) > 0 
                        select t;

投掷错误-无法将运算符应用 ! 对操作数的类型int

如何添加 ! 运算符在我 LINQ 查询?

解决方法 1:

这将为你工作。你不需要使用 & 运算符

vAdvanceSearchResults = from t 
                        in vAdvanceSearchResults 
                        where t.ReadershipLevels.Value != level && t.ReadershipLevels.Value > 0 
                        select t;
官方微信
官方QQ群
31647020