Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to Entities and null strings

I've got quite a strange thing happening on an ASP.NET 4.0 web application using EF 4.0 as its database backend. Essentially, I've got a table that stores users' password reset requests (containing a reset key of type byte[], an expiry of type DateTime, and a foreign key to a User containing a string Email and string Name). Some users do not have an email address set, so for a PasswordRequest request, request.Email is null.

Here's the problem. This works perfectly fine:

string u = Request["u"];
string e = Request["e"];

var requests = from r in context.PasswordRequests
               where r.User.Name == u && r.User.Email == null && r.Expiry >= DateTime.Now
               select r;

I get the expected number of results (nonzero, since there are entries with null emails).

But this always returns an empty collection when e is null:

string u = Request["u"];
string e = Request["e"];

var requests = from r in context.PasswordRequests
               where r.User.Name == u && r.User.Email == e && r.Expiry >= DateTime.Now
               select r;

The only thing that I got to work properly (which doesn't logically make any sense) is this:

string u = Request["u"];
string e = Request["e"];

IQueryable<PasswordRequest> requests;

if (e == null)
    requests = from r in context.PasswordRequests
               where r.User.Name == u && r.User.Email == null && r.Expiry >= DateTime.Now
               select r;
else
    requests = from r in context.PasswordRequests
               where r.User.Name == u && r.User.Email == e && r.Expiry >= DateTime.Now
               select r;

I'm absolutely stumped. Any ideas?

like image 872
hydroiodic Avatar asked Nov 11 '11 07:11

hydroiodic


People also ask

How to handle null in LINQ?

The error might comes from GetAttendanceListOf(), when having methods returning IList or IEnumerable, you should return an empty list if there is no results, it will prevent you from validating every time if it's null or not. Show activity on this post. Show activity on this post. _AttendedDays = (new AttendanceBAL().

How do I check if an entity is not null?

In order to check whether a Java object is Null or not, we can either use the isNull() method of the Objects class or comparison operator.

IS NOT NULL check in Linq?

The query needs to look for the values that is not null in any one of the list values (100 or 110 or 120). model = (from line in db. Bibs where line. TNo == "245" && (line.


1 Answers

Basically this is a mismatch between SQL and C# when it comes to the handling of nulls. You don't need to use two queries, but you need:

where r.User.Name == u && (r.User.Email == e ||
                           (e == null && r.User.Email == null))

It's annoying, and there may be a helper function to make life easier, but it fundamentally comes from SQL's null handling where

where X = Y

will not match if both X and Y are null. (Whereas in C# the equivalent expression would be true.)

You may need to do the same for u as well, unless that is non-nullable in the database.

One small trick you could at least try if you're happy with null and empty strings being handled the same way is:

// Before the query
e = e ?? "";

// In the query
where r.User.Name == u && (r.User.Email ?? "") == e

I believe that will perform null coalescing on both the email column and e, so you never end up comparing null with anything.

like image 106
Jon Skeet Avatar answered Nov 05 '22 18:11

Jon Skeet