Click here to Skip to main content
15,883,811 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
Hi,

Let me explain my question in a simple example.


I have a table named "Person" in mssql.
I am using entity framework 5.0

Person
Id Name Number
1 Tom 5
2 Mary NULL
3 Henry 8

"Number" field is a nullable field.

When I run the code below, it finds the person "Mary" as I want and shows us "Person with Null Number exists!";

C#
using (Entities entities = new Entities())
{
	if (entities.Person.Where(p => p.Number == null).Any())
	{
		MessageBox.Show("Person with Null Number exists!");
	}
	else
	{
		MessageBox.Show("Person with Null Number doesn't exist!");
	}
}



but when I change code to below, it doesnot work same, it returns false and shows us "Person with Null Number doesn't exist!"

C#
int? nullValue = null;

using (Entities entities = new Entities())
{
	if (entities.Person.Where(p => p.Number == nullValue).Any())
	{
		MessageBox.Show("Person with Null Number exists!");
	}
	else
	{
		MessageBox.Show("Person with Null Number doesn't exist!");
	}
}


What is the diffence between them?
Could you help me about this?

Thanks in advance
Posted
Comments
Herman<T>.Instance 18-Jun-14 5:38am    
the diff? NULL vs nullValue!
What is nullValue for kind of object?
ubudak 18-Jun-14 6:04am    
"nullValue" is just the name of the variable.
As you can see in the example, type of it is "Nullable int" (int?)

http://msdn.microsoft.com/en-us/library/ms366789(v=vs.110).aspx

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:
C#
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"); // prints
                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"); // prints
                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.
 
Share this answer
 
v2
as mention the article below, setting "ObjectContextOptions.UseCSharpNullComparisonBehavior Property" to true is enough for solution

http://www.seventy-3.com/entity-framework-and-null-variables/
 
Share this answer
 
Comments
Vedat Ozan Oner 18-Jun-14 6:23am    
ok, then it is not a bug, but I count it as hidden bug because default behavior must be 'true' since it is easy to fall into pit as we see in your case.
ubudak 18-Jun-14 6:28am    
i think it was set to true as default in ef 6.
thank you very much for your comments and answers :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900