I have a problem which the were clause in my LinqToSql Query, here is an extract...
private void GetUsersByDOB(DateTime? DOB)
{
from usr in GetUsers() // returns IQueryable of users
where usr.tblContact.DateOfBirth == (DOB != null ? DOB : usr.tblContact.DateOfBirth)
select usr;
}
The query works when a DOB is passed in (weather its null or has a value).... but the problem is, if the data in the DB is NULL, these are never returned whatever the input is.
It's to do with the evaluation of "where usr.tblContact.DateOfBirth == usr.tblContact.DateOfBirth" - am i doing this correct?
im not sure the first suggestion is correct because that will ALWAYS return null values.... this is part of a search engine and I only want null values returned if the input (DOB) is null.
However, as it stands, the null entries in the DB are never returned whatever the input is.
The second comment, yep this is what I thought, but it doesn't give me my DateTime fields from the database that have a NULL value in them.
s34n
Member
122 Points
36 Posts
LinqToSql where clause not matching null
Sep 30, 2010 09:54 AM|LINK
Hi There,
I have a problem which the were clause in my LinqToSql Query, here is an extract...
private void GetUsersByDOB(DateTime? DOB)
{
from usr in GetUsers() // returns IQueryable of users
where usr.tblContact.DateOfBirth == (DOB != null ? DOB : usr.tblContact.DateOfBirth)
select usr;
}
The query works when a DOB is passed in (weather its null or has a value).... but the problem is, if the data in the DB is NULL, these are never returned whatever the input is.
It's to do with the evaluation of "where usr.tblContact.DateOfBirth == usr.tblContact.DateOfBirth" - am i doing this correct?
Thanks
ignatandrei
All-Star
134521 Points
21576 Posts
Moderator
MVP
Re: LinqToSql where clause not matching null
Sep 30, 2010 10:47 AM|LINK
usr.tblContact.DateOfBirth is null or usr.tblContact.DateOfBirth == (DOB != null ? DOB : usr.tblContact.DateOfBirth)
mylok
Member
436 Points
176 Posts
Re: LinqToSql where clause not matching null
Sep 30, 2010 12:57 PM|LINK
I think "where usr.tblContact.DateOfBirth == usr.tblContact.DateOfBirth" is correct. You can use sqlprofile or linqpad check the sql code.
From your when DOB is null will return all users is that what you want?
s34n
Member
122 Points
36 Posts
Re: LinqToSql where clause not matching null
Oct 01, 2010 07:45 AM|LINK
Thanks for the reply...
im not sure the first suggestion is correct because that will ALWAYS return null values.... this is part of a search engine and I only want null values returned if the input (DOB) is null.
However, as it stands, the null entries in the DB are never returned whatever the input is.
The second comment, yep this is what I thought, but it doesn't give me my DateTime fields from the database that have a NULL value in them.
mylok
Member
436 Points
176 Posts
Re: LinqToSql where clause not matching null
Oct 01, 2010 03:31 PM|LINK
"it doesn't give me my DateTime fields from the database that have a NULL value in them "
when you run your query did you get any data return except the NULL?
s34n
Member
122 Points
36 Posts
Re: LinqToSql where clause not matching null
Oct 04, 2010 07:22 AM|LINK
Yep, all the other data is still returned
mylok
Member
436 Points
176 Posts
Re: LinqToSql where clause not matching null
Oct 04, 2010 01:42 PM|LINK
Did you debug the DOB value you pass to GetUsersByDOB method?
It seems nothing wrong with your query, I suspect the DOB value you pass to the method never be null