LINQ Where() Single or Double Pipes / Ampersands
c#, linq, linq-to-sql
Solution
You want `||` / `&&`.
Well, the single-pipe (`|` / `&`) is generally used for bitwise arithmetic, and (among other problems) may make the code base harder to understand; it'll work in LINQ-to-Objects (since bitwise is still defined for `bool`), but without the usual short-circuiting. But if your data source is a database (i.e. there is an expression parser in the mix), you may find it explodes on you.
OK; bitwise may have been misleading; but || and && remains the most logical and expected way of expressing your intent. My apologies for any confusion.
Problem
Anyone care to comment on whether we should be using "I" or "II" and "&" or "&&" in our LINQ Where() extensions / queries? Any difference with LINQ to SQL? The resulting expression tree is more than I can get my brain around on a Friday afternoon Thanks, ``` static void Main(string[] args) { var numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var q1 = numbers.Where(i => i == 1 | i == 2); var q2 = numbers.Where(i => i == 1 || i == 2); var q3 = numbers.Where(i => i == 1 & i < 3); var q4 = numbers.Where(i => i == 1 && i < 3); Write(q1); Write(q2); Write(q3); Write(q4); } static void Write<T>(IEnumerable<T> t) { foreach (var i in t) Console.Write("{0} ", i); Console.WriteLine(); } Results: 1 2 1 2 1 1 ```