Page 1 of 1

Wrong evaluation of && and || operator in LINQ queri

Posted: Tue 26 May 2009 10:14
Hello

I am using the || (OR) operator in the where clause of a LINQ statement. If the first oparand evaluates to true, the second operand shouldn't be evaluated as in the following example...

Code: Select all

			string searchString = null;

			if (true || "test".Contains(searchString))
				searchString = "";
within this LINQ statement this rule does not work anymore... the second operand is evaluated anyway and throws an exception...

Code: Select all

			string searchString = null;
			var recordSet = (from m in PPCustomer.Mailings
							 where String.IsNullOrEmpty(searchString) || m.Subject.Contains(searchString)
							 select m);
Can you confirm, this is a bug?

Best regards
________
Subaru R1 Specifications

Posted: Tue 26 May 2009 12:17
by AndreyR
The problem is more likely associated with the translation of the Contains() method.
We will investigate the problem.

Posted: Tue 26 May 2009 13:24
Yes, the Contains method throws an exception if the argument is null. But anyway... if the || operator would work as expected (short-circuit evaluation) the Contains method wouldn't be evaluated and therefore wouldn't throw an exception.
http://msdn.microsoft.com/en-us/library ... S.80).aspx

best regards
________
TRICHOMES