There must be a bug in linq-to-entities for version 5.0. I have tested it with version 6.1 and I didn't observe such thing.
here is the code:
class Program
{
static void Main(string[] args)
{
int? i1 = null;
using (TESTEntities ctx = new TESTEntities())
{
ctx.Database.Log = Console.Write;
var exists = ctx.TABLE_A.Where(e1 => e1.A_NUM == null).Any();
if(exists)
Console.WriteLine("exists");
else
Console.WriteLine("not exists");
Console.ReadKey();
}
using (TESTEntities ctx = new TESTEntities())
{
ctx.Database.Log = Console.Write;
var exists = ctx.TABLE_A.Where(e1 => e1.A_NUM == i1).Any();
if (exists)
Console.WriteLine("exists");
else
Console.WriteLine("not exists");
Console.ReadKey();
}
Console.ReadKey();
}
}
and the output:
Opened connection at 18/06/2014 12:59:32 PM +03:00
SELECT
CASE WHEN ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[TABLE_A] AS [Extent1]
WHERE [Extent1].[A_NUM] IS NULL
)) THEN cast(1 as bit) WHEN ( NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[TABLE_A] AS [Extent2]
WHERE [Extent2].[A_NUM] IS NULL
)) THEN cast(0 as bit) END AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
-- Executing at 18/06/2014 12:59:33 PM +03:00
-- Completed in 16 ms with result: SqlDataReader
Closed connection at 18/06/2014 12:59:33 PM +03:00
exists
Opened connection at 18/06/2014 1:00:37 PM +03:00
SELECT
CASE WHEN ( EXISTS (SELECT
1 AS [C1]
FROM [dbo].[TABLE_A] AS [Extent1]
WHERE ([Extent1].[A_NUM] = @p__linq__0) OR (([Extent1].[A_NUM] IS NULL)
AND (@p__linq__0 IS NULL))
)) THEN cast(1 as bit) WHEN ( NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[TABLE_A] AS [Extent2]
WHERE ([Extent2].[A_NUM] = @p__linq__0) OR (([Extent2].[A_NUM] IS NULL)
AND (@p__linq__0 IS NULL))
)) THEN cast(0 as bit) END AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
-- p__linq__0: 'null' (Type = Int32)
-- Executing at 18/06/2014 1:00:37 PM +03:00
-- Completed in 9 ms with result: SqlDataReader
Closed connection at 18/06/2014 1:00:37 PM +03:00
exists
you can either use sql profiler or context.Database.Log to see the actual query. it will show what happens.