Entity Framework queries skipped filtered index WHERE BIT field = 0

I noticed that the Entity Framework translates LINQ queries with negative Boolean filters, so that the generated query plan will not use the filtered index. For example, the query:

context.Foo.Count(f => !f.IsActive)

generates an SQL statement:

SELECT 
    [GroupBy1].[A1] AS [C1]
    FROM ( SELECT 
        COUNT(1) AS [A1]
        FROM [dbo].[Foos] AS [Extent1]
        WHERE [Extent1].[IsActive] <> cast(1 as bit)
    )  AS [GroupBy1]

Note that the sentence WHEREuses [IsActive] <> cast(1 as bit), but not more intuitively [IsActive] = 0. This becomes a problem when using filtered indexes. In terms of the above query, the following index will not be used:

CREATE INDEX IX_Foo_IsActive ON Foos (IsActive) WHERE (IsActive = 0)

, , EF , - DB, , . , EF (IsActive <> 1) , -EF- .

?

: http://dotnetfiddle.net/3kZugt. :

public class Foo
{
    public int Id { get; set; }
    public bool IsActive { get; set; }
}
+4
1

, - , : #, ..

WHERE IsActive = 0

f => f.IsActive = false

# SQL;)

+6

Source: https://habr.com/ru/post/1538803/


All Articles