Incorrect SQL generation on query

丶灬走出姿态 提交于 2019-12-24 01:13:23

问题


Dim records = From record In db.table
              Where record.Level = "Level 1"
              Where record.NewNumber IsNot Nothing
              Select record.Number

When I call records.ToString() I get an output like so:

SELECT [Extent1].[Number] AS [Number] FROM [dbo].[table] AS [Extent1] WHERE [Extent1].[Level] = @p__linq__0

The where clause for NewNumber is not null is missing. If I change the linq to where NewNumber is nothing I get the following:

SELECT CAST(NULL AS varchar(1)) AS [C1] FROM ( SELECT 1 AS X ) AS [SingleRowTable1] WHERE 1 = 0

What is going on here? The number field is my primary key and newnumber is just another field. I've only changed some of the names/values for this post. Executing both where clauses on the same line with And/AndAlso makes no difference.

The field NewNumber is configured as such.

[Property](Function(x) x.NewNumber).HasMaxLength(20).HasColumnName("Newnumber").IsOptional()


回答1:


EF knows that NewNumber can't be null/nothing and either ignores the predicate (IsNot Nothing) because it's always true, or when it's always false (Is Nothing) it turns it into something that's easier to evaluate for the query optimizer.



来源:https://stackoverflow.com/questions/49537627/incorrect-sql-generation-on-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!