问题
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