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?
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().
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With