I thought the query was quite trivial, but it’s raising a FormatException (“Input string was not in a correct format”) nonetheless:
Submissions.Where(s => (false && s.Status == Convert.ToInt16("")))
(of course, in my code, another expression that evaluates to ‘false’ is located before ‘&&’)
So why is the part after ‘&&’ evaluated, since the first part is always false and the total expression can never evaluate to true?
The situation is particularly strange because only the Convert.ToInt16("") part seems to raise an exception – other parts of my original query of more or less the same structure, like
Submissions.Where(s => (false && s.SubmissionDate <= DateTime.Now))
are evaluated correctly.
As the others have pointed out, LINQ to SQL code gets pulled apart into an expression tree before being run as SQL code against the database. Since SQL does not necessarily follow the same short-circuit boolean rules as C#, the right side of your expression code might get parsed so that the SQL can be constructed.
From MSDN:
As for why you’re getting an exception with this code,
Convert.ToInt16("")will always throw precisely that exception because there’s no way to convert an empty string into an integer. Your other example doesn’t attempt an invalid conversion, hence it runs without a problem.