Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Where() Single or Double Pipes / Ampersands

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
like image 368
andleer Avatar asked Jul 31 '09 20:07

andleer


People also ask

Is || the same as or?

& and && indicate logical AND and | and || indicate logical OR. The shorter forms performs elementwise comparisons in much the same way as arithmetic operators. The longer forms evaluates left to right, proceeding only until the result is determined.

What is the return type of Where in LINQ?

There are only two return types for a Linq query. It's either a single concrete object or a single anonymous type object that is returned. It can also be a List<T> of concrete objects or anonymous type objects that are returned in a collection.

What is where clause in LINQ?

The where clause is used in a query expression to specify which elements from the data source will be returned in the query expression. It applies a Boolean condition (predicate) to each source element (referenced by the range variable) and returns those for which the specified condition is true.

What is meaning of |= in C#?

Equality operator (==), returns true for operands whose values are equal. Inequality operator (! =), returns false if two operands are equal. Less than relational operator (<), defined for all numeric and enumeration types and returns true if the first operand is less than the second operand.


2 Answers

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.

like image 143
Marc Gravell Avatar answered Oct 25 '22 03:10

Marc Gravell


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), 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.

like image 38
Amy B Avatar answered Oct 25 '22 04:10

Amy B