Entity Framework processing of null values

Source: Internet
Author: User

Today, I used Entity Framework to rewrite an earlier project. When it involved processing null values, I encountered a problem: how to query records with a null field in the database, I wrote several examples for testing and then used SQL Server Profiler to capture SQL.


Test 1: Use null directly

 
  
  
  1. VaR clients = CTX. Clients. Where (C => C. parentguid = NULL );

The corresponding SQL statement is:

 
  
  
  1. -- Normal execution
  2. Select
  3. [Extent1]. [guid] as [guid],
  4. [Extent1]. [name] as [name],
  5. [Extent1]. [parentguid] as [parentguid]
  6. From [DBO]. [clients] as [extent1]
  7. Where [extent1]. [parentguid] is null



Test 2: Use a variable equal to NULL:

 
  
  
  1. String parentguid = NULL;
  2. VaR clients = CTX. Clients. Where (C => C. parentguid = parentguid );

The corresponding SQL statement is:

  
  
  1. -- A null variable is used, but an equal sign is used, so the value cannot be obtained.
  2. Exec sp_executesql n' select
  3. [Extent1]. [guid] as [guid],
  4. [Extent1]. [name] as [name],
  5. [Extent1]. [parentguid] as [parentguid]
  6. From [DBO]. [clients] as [extent1]
  7. Where [extent1]. [parentguid] = @ P _ LINQ _ 0', n' @ P _ LINQ _ 0 varchar (8000) ', @ P _ LINQ _ 0 = NULL



Test 3: Add judgment

 
  
  
  1. String parentguid = NULL;
  2. VaR clients = CTX. Clients. Where (C => C. parentguid = (string. isnullorempty (parentguid )? Null: parentguid ));

The corresponding SQL statement is:

  
  
  1. -- Speechless. This is too complicated. Obviously, the correct result is not obtained.
  2. Exec sp_executesql n' select
  3. [Extent1]. [guid] as [guid],
  4. [Extent1]. [name] as [name],
  5. [Extent1]. [parentguid] as [parentguid]
  6. From [DBO]. [clients] as [extent1]
  7. Where [extent1]. [parentguid] = (
  8. Case
  9. When (@ P _ LINQ _ 0 is null) or (cast (LEN (@ P _ LINQ _ 0) as INT) = 0 ))
  10. Then cast (null as varchar (1 ))
  11. Else @ P _ LINQ _ 1
  12. End
  13. ) ', N'@ P _ LINQ _ 0 nvarchar (4000), @ P _ LINQ _ 1 nvarchar (4000 )', @ P _ LINQ _ 0 = NULL, @ P _ LINQ _ 1 = NULL



Test 4: Add judgment in another way

  
  
  1. String parentguid = NULL;
  2. VaR clients = CTX. Clients. Where (C => string. isnullorempty (parentguid )? C. parentguid = NULL: C. parentguid = parentguid );

The corresponding SQL statement is:

 
 
  1. -- Continue to be speechless, which is more complex than the previous one and cannot get the correct results.
  2. Exec sp_executesql n' select
  3. [Extent1]. [guid] as [guid],
  4. [Extent1]. [name] as [name],
  5. [Extent1]. [parentguid] as [parentguid]
  6. From [DBO]. [clients] as [extent1]
  7. Where (
  8. Case
  9. When (@ P _ LINQ _ 0 is null) or (cast (LEN (@ P _ LINQ _ 0) as INT) = 0 ))
  10. Then cast (0 as bit)
  11. When ([extent1]. [parentguid] = @ P _ LINQ _ 1)
  12. Then cast (1 as bit)
  13. When ([extent1]. [parentguid] <> @ P _ LINQ _ 1)
  14. Then cast (0 as bit)
  15. End
  16. ) = 1', n' @ P _ LINQ _ 0 nvarchar (4000), @ P _ LINQ _ 1 varchar (8000 )', @ P _ LINQ _ 0 = NULL, @ P _ LINQ _ 1 = NULL

Fainting, it seems that it is impossible to set the idea of LINQ directly to EF. In some cases, the difference is quite large.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.