Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework null value in query

I'm seeing a strange behavior in an EF query adn I'm wondering why it is happening. With the following code I don't get any results:

if (category.Parent == null)
{
    return Db.EventCategories.Where(c => c.Parent == category.Parent);
}

But with this code it does return the expected results:

if (category.Parent == null)
{
    return Db.EventCategories.Where(c => c.Parent == null);
}

What is the difference? Isn't null always null? or does the EF treats them as different elements when the value is a nullable (Parent is of type int?).

like image 553
willvv Avatar asked Nov 05 '22 16:11

willvv


1 Answers

I'm not 100% sure, but I think the first statement generates something like SELECT ... FROM category, eventcategories WHERE category.parent = eventcategories.parent (which returns empty recordset if category.parent is null), whereas the second ... WHERE eventcategories.parent IS NULL.

like image 118
a1ex07 Avatar answered Nov 14 '22 22:11

a1ex07