I believe there is some uncertainty around zeros with some LINQ providers. Try:
var query = db.Pages.First(x => (test != null && x.ParentId == test) || (test == null && x.ParentId == null));
Alternatively, use different queries for different situations:
var query = test == null ? db.Pages.First(x => x.ParentId == null) : db.Pages.First(x => x.ParentId == test);
This is mainly because SQL treats NULL as not equal to itself, therefore:
WHERE X = Y
will still fail if both X and Y are null values. Using the == null
part (with a literal zero) results in conversion to ISNULL
or regardless of the SQL equivalent.
I agree that this is a pain, and someone else may have a better workaround, but it can help you get away.
source share