Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between equals (LINQ) and == (C#) operators?

Tags:

c#

linq

I have been reading Programming Microsoft LINQ in Microsoft .NET Framework 4, and now I am understanding the join clause in LINQ, but I have a doubt or question respect about its definition; in the book it is defines as:

You can define equality comparisons only by using a special equals keyword that behaves differently from the == operator, because the position of the operands is significant. With equals, the left key consumes the outer source sequence, and the right key consumes the inner source sequence. The outer source sequence is in scope only on the left side of equals, and the inner source sequence is in scope only on the right side.

And there is also a formal definition about this operator:

join-clause ::= join innerItem in innerSequence on outerKey equals innerKey

Please, can someone explain me the above concept in other words or by paraphrasing it?

like image 843
InfZero Avatar asked Nov 10 '14 14:11

InfZero


People also ask

What is the difference between Equals () and == C#?

Difference between == and . Equals method in c# The Equality Operator ( ==) is the comparison operator and the Equals() method in C# is used to compare the content of a string. The Equals() method compares only content.

What is the difference between Equals () method and == operator?

equals() method. The major difference between the == operator and . equals() method is that one is an operator, and the other is the method. Both these == operators and equals() are used to compare objects to mark equality.

Which is better Equals or == in C#?

Answering to the point “There is no difference between equality comparison using “==” and “Equals()”, except when you are comparing “String” comparison. The common comparison Rule :-Whenever youare comparing variables they are either value types or reference types.

What is the difference between A Equals B and a == b?

The Equals() and == operator are used for comparison and both returns the boolean value (true/false). For Value Type == and Equals() works in the same way, both compare two object by value. a==b and a. Equals(b) returns true , because in this case both compare two object by value.


1 Answers

I guess it's because 'equals' in the join doesn't work like == does, and so the language designers decided not to call what it is doing the same thing.

In C#, it is sort of given that a == b is exactly the same as b == a. In the definition of a join, this is not so:

var list = from a in ctx.TableA
           join b from ctx.TableB on a.Id equals b.tableAId

This, above, is valid.

var list = from a in ctx.TableA
           join b from ctx.TableB on b.tableAId equals a.Id

This will not compile. What the language spec says is that the 'outer' table (TableA in this case) must be specified first and the inner one (TableB) must be second. I suppose that the language designers thought that this was sufficiently different from the way that == works that it would be a bad idea to use it and they came up with the idea to use 'equals'.

I think I'm probably right, but only the language designers involved will really know the truth.

like image 97
simon at rcl Avatar answered Oct 09 '22 18:10

simon at rcl