sql列ReadershipLevels
- int
,带空
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 to 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
运算符?
答案 0 :(得分:2)
这对你有用。你不需要使用&amp;操作
vAdvanceSearchResults = from t
in vAdvanceSearchResults
where t.ReadershipLevels.Value != level && t.ReadershipLevels.Value > 0
select t;
答案 1 :(得分:1)
尝试更改:where ! (t.ReadershipLevels.Value & level) > 0
对此:where ! ((t.ReadershipLevels.Value & level) > 0 )
答案 2 :(得分: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;
或者,如果您不想显示readerhiplevels值为“32”的记录,请执行简单的检查,
vAdvanceSearchResults = from t
in vAdvanceSearchResults
where (t.ReadershipLevels ?? 0) != 32
select t;
希望有所帮助