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

Discussion of open issues, suggestions and bugs regarding LinqConnect – Devart's LINQ to SQL compatible ORM
Post Reply
[email protected]
Posts: 43
Joined: Wed 17 Sep 2008 11:31

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

Post by [email protected] » 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
Last edited by [email protected] on Thu 17 Feb 2011 05:22, edited 1 time in total.

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Tue 26 May 2009 12:17

The problem is more likely associated with the translation of the Contains() method.
We will investigate the problem.

[email protected]
Posts: 43
Joined: Wed 17 Sep 2008 11:31

Post by [email protected] » 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

Post Reply