views:

582

answers:

4

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

Press any key to continue . . .
+6  A: 

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.

Marc Gravell
`|` isn't bitwise for `bool`s, it's just not short-circuiting. That said, does SQL even guarantee that `AND` and `OR` short-circuit there? If it does not, then I'd expect `|` and `||` to both translate to `OR` in any case.
Pavel Minaev
Thanks. Basically what I was thinking. Marc +1, John ?
Andrew Robinson
Ok Pavel, Marc, which is it? Other than the guy's code that I am reviewing, I have never seen single versions of either used.
Andrew Robinson
blesh
Pavel Minaev
+1  A: 

It is all the same in your example. The | operator is overloaded for the type bool and is the same as ||

http://msdn.microsoft.com/en-us/library/kxszd0kx.aspx

I prefer using || because people are used to it and it feels more natural to most developers.

Stilgar
It's not the same - `|` on booleans is not short-circuit (i.e. it always evaluates right side), while `||` is. The question here is whether it matters in LINQ to SQL context.
Pavel Minaev
I learn 2 things about C# from you in a day. I'm impressed:)
Stilgar
A: 

It really depends on your preference.

In general MOST developers stick to || and && for all logical OR's and AND's especially when they're dealing with a bunch of numbers in their code, as you are. This is because it is visually confusing to other developers.

So, my opinion is to stick to || and && for your logical work and only use | and & for your bitwise operations.

Again, though it's just preference.

blesh
+1  A: 

Anyone care to comment on whether we should be using | or || and & or && in our LINQ Where() extensions / queries?

In lambda expressions (such as those frequently used in Linq To Sql), these operators act as they normally do in c#. | and & are logical operators while || and && are short-circuiting logical operators. Short circuiting is good if you want efficient code. Short-circuiting is bad if you want to avoid branching code (perhaps to UnitTest with 100% coverage). Most people use short-circuiting all the time and the branching doesn't bother them because they avoid using expressions that have side effects, thus they suffer no ill consequences when some of the expressions are not evaluated.

Here's an example of useful branching by short circuiting:

if (DatabaseIsAvailable() && QueryDataAndThereAreResults())
{
  //do something with Results
}

When DatabaseIsAvailable() is false, QueryDataAndThereAreResults() will not be evaluated.

Any difference with LINQ to SQL?

It doesn't matter which you use for Linq to Sql. The punctuation will be translated into T-Sql's AND and OR operators. After the query is sent to the database, the SqlServer Query Plan Optimizer will figure out whether or not to short circuit.

David B