0

sql column ReadershipLevels - int with null

level is local int type variable

& is bitwise operator

Linq to SQL

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

if level = 32

Above Linq query generated the SQL like below:

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]

I want to include a not So I made like below

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]

Which is working fine.

Now I want to change same in LINQ to SQL also

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

But the above throwing error- cannot apply operator ! to operand of type System.Nullable

Changed to -

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

Throwing error- Cannot apply operator ! to operand of type int

How can I add a ! operator in my LINQ query?

4

3 回答 3

2

这对你有用。您不需要使用 & 运算符

vAdvanceSearchResults = from t 
                        in vAdvanceSearchResults 
                        where t.ReadershipLevels.Value != level && t.ReadershipLevels.Value > 0 
                        select t;
于 2015-05-27T06:11:51.783 回答
1

尝试改变: where ! (t.ReadershipLevels.Value & level) > 0
到这个: where ! ((t.ReadershipLevels.Value & level) > 0 )

于 2015-05-27T04:59:57.187 回答
1

为什么我们在这里需要一个位运算符?

如您的问题中所述,如果您使用该条件,

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

它将检索 ReadershipLevels 值在 (1 到 31)、(64 到 95)、(128 到 159) 等中的所有结果。

如果您真的想显示上述范围内的记录,您可以使用如下所示,not ([t2].[ReadershipLevels] & 32) > 0与检查它是否等于 0 相同,

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

或者,如果您不想显示 readershiplevels 值为“32”的记录,请进行简单检查,

vAdvanceSearchResults = from t 
                    in vAdvanceSearchResults 
                    where (t.ReadershipLevels ?? 0) != 32 
                    select t;

希望能帮助到你

于 2015-05-27T05:10:22.917 回答